tags:

views:

848

answers:

3

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__)
+10  A: 

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

Maciej Pasternacki
+2  A: 

Also check out the built-in inspect module. It can be very handy.

DrBloodmoney
+2  A: 

As previously mentioned, globals gives you a dictionary as opposed to dir() which gives you a list of the names defined in the module. The way I typically see this done is like this:

import sys
dir(sys.modules[__name__])
jls