Fix Python – How to get a reference to current module’s attributes in Python

Question

Asked By – guillermooo

What I’m trying to do would look like this in the command line:

>>> import mymodule
>>> names = dir(mymodule)

How can I get a reference to all the names defined in mymodule from within mymodule itself?

Something like this:

# mymodule.py
names = dir(__thismodule__)

Now we will see solution for issue: How to get a reference to current module’s attributes in Python


Answer

Just use globals()

globals() — Return a dictionary
representing the current global symbol
table. This is always the dictionary
of the current module (inside a
function or method, this is the module
where it is defined, not the module
from which it is called).

http://docs.python.org/library/functions.html#globals

This question is answered By – Maciej Pasternacki

This answer is collected from stackoverflow and reviewed by FixPython community admins, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0