attribut of the current module

Alex Martelli aleax at
Fri Sep 21 09:38:22 CEST 2001

"Oleg Orlov11980884" <orlov at> wrote in message
news:9oeo0n$28vn$1 at
> How can i get attribut of the current module?
> Ugly way: getattr(__import__(__name__), attribname)

Pretty much the same:
    getattr(sys.modules[__name__], attribname)

If you have the attribute name as a string variable,
you won't be able to do much more than getattr in any
case.  The only issue left is how to get the current
module object, and sys.modules[__name__] is the normal
approach for that.

A different, simpler approach reaching the same goal:


globals() returns the _dictionary_ of the current
module, so you simply subscript it.


More information about the Python-list mailing list