All names in the current module

Lawrence Oluyede raims at dot.com
Wed Aug 15 13:01:17 EDT 2007


Torsten Bronger <bronger at physik.rwth-aachen.de> wrote:
> How can I get a list with all classes defined in the current module?
> Thank you!

rhymes at groove ~ % cat > t.py
class A: pass

rhymes at groove ~ % python
Python 2.5.1 (r251:54869, Apr 18 2007, 22:08:04) 
[GCC 4.0.1 (Apple Computer, Inc. build 5367)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import t
>>> print dir(t)
['A', '__builtins__', '__doc__', '__file__', '__name__']

Now you have the list of names. To find out if they are actual classes
or not you can do this:

>>> import inspect
>>> for member in dir(t):
...     print member, inspect.isclass(getattr(t, member))
... 
A True
__builtins__ False
__doc__ False
__file__ False
__name__ False

HTH

-- 
Lawrence, oluyede.org - neropercaso.it
"It is difficult to get a man to understand 
something when his salary depends on not
understanding it" - Upton Sinclair



More information about the Python-list mailing list