[Python-3000] PEP 30xx: Access to Module/Class/Function Currently Being Defined (this)

Steven Bethard steven.bethard at gmail.com
Mon Apr 23 16:19:12 CEST 2007


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::
>
>     __import__(__name__)
>
> 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.

STeVe
-- 
I'm not *in*-sane. Indeed, I am so far *out* of sane that you appear a
tiny blip on the distant coast of sanity.
        --- Bucky Katt, Get Fuzzy


More information about the Python-3000 mailing list