How can a module find its own module object?

Martin von Loewis loewis at informatik.hu-berlin.de
Sun Dec 23 19:04:04 EST 2001


Michael Hudson <mwh at python.net> writes:

> > Is there any other way for a module to find itself other than searching 
> > sys.modules for a module whose __dict__ is globals()?
> 
> What's wrong with __file__?

That doesn't give you the module object. The module object is not
readily accessible from the module itself, as that would create a
cyclic reference. Looking for an entry in sys.modules that has the
same __file__ might be more straight-forward than comparing __dict__
to globals(), though (there is no guarantee that globals() wouldn't
return a fresh dictionary - although all current implementations do
return __dict__).

Regards,
Martin




More information about the Python-list mailing list