How to get all variables of some module in that module
Steven D'Aprano
steven at REMOVE.THIS.cybersource.com.au
Tue Oct 21 01:53:43 EDT 2008
On Tue, 21 Oct 2008 11:56:30 +0700, Alex Gusarov wrote:
> Here I want to get all Table instances of current module and put them
> into dictionary by names, but I don't know how I can get all variables
> of current module in the end of this module. Please, give me a hint.
>>> import math
>>> dir(math)
['__doc__', '__file__', '__name__', 'acos', 'asin', 'atan', 'atan2',
'ceil', 'cos', 'cosh', 'degrees', 'e', 'exp', 'fabs', 'floor', 'fmod',
'frexp', 'hypot', 'ldexp', 'log', 'log10', 'modf', 'pi', 'pow',
'radians', 'sin', 'sinh', 'sqrt', 'tan', 'tanh']
Note that dir() gives you a list of "interesting" names. You can also use
math.__dict__.keys() or the inspect module.
Once you have a key, you can get the actual object you want with getattr:
>>> key = 'acos'
>>> getattr(math, key)
<built-in function acos>
--
Steven
More information about the Python-list
mailing list