[Python-3000] PEP 30xx: Access to Module/Class/Function Currently Being Defined (this)
exarkun at divmod.com
Mon Apr 23 16:53:32 CEST 2007
On Mon, 23 Apr 2007 08:19:12 -0600, Steven Bethard <steven.bethard at gmail.com> wrote:
>On 4/23/07, Steven Bethard <steven.bethard at gmail.com> wrote:
>> On 4/23/07, Barry Warsaw <barry at python.org> wrote:
>> > -----BEGIN PGP SIGNED MESSAGE-----
>> > Hash: SHA1
>> > On Apr 23, 2007, at 9:21 AM, Christian Heimes wrote:
>> > > -1 on __module__
>> > >
>> > > I understand PEP 3020 correctly then sys.main will contain the dotted
>> > > name of the module as *string*.
>> > >
>> > > Also __module__ is already used in objects like classes.
>> > I'm not sure whether a general mechanism is necessary or not, but
>> > I've always wished I could just do something like "import __me__" to
>> > get the current module.
>> Well, you can always do::
>> That's not much more verbose.
>Of course, to make it work with packages, you have to write something like::
> mod = __import__(__name__)
> for sub_mod_name in __name__.split('.')[1:]:
> mod = getattr(mod, sub_mod_name)
>I guess that's not so simple after all.
Maybe this indicates a problem with __import__. Twisted has something
called namedAny, and I always use that, because it just works, unlike
__import__, which seems to take too many arguments and has weird behavior
for dotted names.
me = namedAny(__name__)
The inverse is also available (qual(me) == __name__), and is another
function I've always thought it strange was missing from the stdlib.
More information about the Python-3000