Module access from inside itself

Discussion in 'Python' started by Steven, Aug 15, 2003.

  1. Steven

    Steven Guest

    I'm writing a Python script which can be called from the command-line, and
    I want to use my module doc string as the CL help text, but I don't know
    how to access my module object from inside the module.

    I've tried searching for an answer, but haven't found anything. Please
    excuse me if I'm missing something simple, I'm a newbie to Python.

    I'm doing something like this:



    #!/usr/bin/python
    """Module doc string goes here.
    """

    import getopt, sys

    def MyFunction(args):
        pass

    if __name__ == "__main__":
        opts, args = getop.getopt(sys.argv[1:], [], ["help"])
        for opt in opts:
            if opt == "--help":
                print MY_MODULE.__doc__    # How do I get this?
            else:
                MyFunction(args)



    How do I get a reference to the module from inside the module? Is this
    the Pythonic way of generating help strings for CL scripts?


    Thanks,


    --
    Steven D'Aprano
    Steven, Aug 15, 2003
    #1
    1. Advertising

  2. Steven

    Oren Tirosh Guest

    On Fri, Aug 15, 2003 at 04:20:43PM +0000, Steven wrote:
    > I'm writing a Python script which can be called from the command-line, and
    > I want to use my module doc string as the CL help text, but I don't know
    > how to access my module object from inside the module.


    You don't need the module object for this. MY_MODULE.__doc__ is the
    same as accessing __doc__ directly.

    If you really need the current module object use sys.modules[__name__]

    Oren
    Oren Tirosh, Aug 15, 2003
    #2
    1. Advertising

  3. Steven wrote:

    > I'm writing a Python script which can be called from the command-line, and
    > I want to use my module doc string as the CL help text, but I don't know
    > how to access my module object from inside the module.


    How about this:

    # demo of accessing the docstring:

    """
    This is a test.

    Had this been a real document, it would have said something.
    """


    if __name__ == '__main__':
    import __main__
    print __main__.__doc__
    Scott David Daniels, Aug 15, 2003
    #3
  4. On Fri, 15 Aug 2003 16:20:43 GMT, Steven <> wrote:

    >I'm writing a Python script which can be called from the command-line, and
    >I want to use my module doc string as the CL help text, but I don't know
    >how to access my module object from inside the module.
    >
    >I've tried searching for an answer, but haven't found anything. Please
    >excuse me if I'm missing something simple, I'm a newbie to Python.
    >
    >I'm doing something like this:
    >
    >
    >
    >#!/usr/bin/python
    >"""Module doc string goes here.
    >"""
    >
    >import getopt, sys
    >
    >def MyFunction(args):
    >    pass
    >
    >if __name__ == "__main__":
    >    opts, args = getop.getopt(sys.argv[1:], [], ["help"])
    >    for opt in opts:
    >        if opt == "--help":
    >            print MY_MODULE.__doc__    # How do I get this?

                print __doc__    # should get it

    >        else:
    >            MyFunction(args)
    >
    >
    >
    >How do I get a reference to the module from inside the module? Is this
    >the Pythonic way of generating help strings for CL scripts?
    >

    You are already inside the module, so an unqualified name will refer
    to module globals unless it's being used in some local scope that has
    a binding shadowing the global name.

    Regards,
    Bengt Richter
    Bengt Richter, Aug 15, 2003
    #4
  5. Steven

    Aahz Guest

    In article <>,
    Steven <> wrote:
    >
    >if __name__ == "__main__":
    >    opts, args = getop.getopt(sys.argv[1:], [], ["help"])
    >    for opt in opts:
    >        if opt == "--help":
    >            print MY_MODULE.__doc__    # How do I get this?
    >        else:
    >            MyFunction(args)


    import __main__
    print __main__.__doc__
    --
    Aahz () <*> http://www.pythoncraft.com/

    This is Python. We don't care much about theory, except where it intersects
    with useful practice. --Aahz
    Aahz, Aug 16, 2003
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Bob
    Replies:
    0
    Views:
    286
  2. Bob
    Replies:
    4
    Views:
    926
  3. Tatu Portin

    Using class inside itself

    Tatu Portin, May 30, 2005, in forum: C++
    Replies:
    3
    Views:
    545
    Rolf Magnus
    May 30, 2005
  4. eric_bellard
    Replies:
    1
    Views:
    5,075
    CarlosRivera
    Oct 7, 2004
  5. plb
    Replies:
    2
    Views:
    315
Loading...

Share This Page