Module access from inside itself
Bengt Richter
bokr at oz.net
Fri Aug 15 18:04:10 EDT 2003
On Fri, 15 Aug 2003 16:20:43 GMT, Steven <dippy at mikka.net.au> 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
More information about the Python-list
mailing list