Function getting a reference to its own module
Carl Banks
pavlovevidence at gmail.com
Wed Sep 17 03:44:08 EDT 2008
On Sep 16, 10:29 pm, Steven D'Aprano
<ste... at REMOVE.THIS.cybersource.com.au> wrote:
> On Wed, 17 Sep 2008 13:52:13 +1200, greg wrote:
> > Arnaud Delobelle wrote:
>
> >> Or:
>
> >> import ModuleName as this_module
>
> > Or:
>
> > this_module = __import__(__name__)
>
> > then you don't have to change anything.
>
> I like that solution! And it works regardless of whether the module
> holding it is imported, or is being executed from the commandline.
It doesn't work if the module is part of a package. For that could
use sys.modules[__name__] or write a separate function to return the
appropriate nested module of __name__. Neither method is foolproof I
don't think.
Carl Banks
More information about the Python-list
mailing list