By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,965 Members | 1,631 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,965 IT Pros & Developers. It's quick & easy.

All names in the current module

P: n/a
Hallöchen!

How can I get a list with all classes defined in the current module?
Thank you!

Tschö,
Torsten.

--
Torsten Bronger, aquisgrana, europa vetus
Jabber ID: br*****@jabber.org
(See http://ime.webhop.org for ICQ, MSN, etc.)
Aug 15 '07 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Torsten Bronger <br*****@physik.rwth-aachen.dewrote:
How can I get a list with all classes defined in the current module?
Thank you!
rhymes@groove ~ % cat t.py
class A: pass

rhymes@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
Aug 15 '07 #2

P: n/a
Il Wed, 15 Aug 2007 19:01:17 +0200, Lawrence Oluyede ha scritto:
Torsten Bronger <br*****@physik.rwth-aachen.dewrote:
>How can I get a list with all classes defined in the current module?
Thank you!

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

rhymes@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:

to get names' list you can simply call globals()

bye
Aug 15 '07 #3

P: n/a
Fabio Z Tessitore <fa*************@libero.itwrote:
to get names' list you can simply call globals()
Not strictly true. globals() returns the current's scope global vars. If
you import a module in the current scope globals() won't display the
names inside it.

--
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
Aug 15 '07 #4

P: n/a
Torsten Bronger wrote:
Hallöchen!

How can I get a list with all classes defined in the current module?
Thank you!

Tschö,
Torsten.
Assuming you want to see all classes in the re module:
>>import re
help(re) #best way

def isclass(cls):
.... try:
.... return issubclass(cls, cls)
.... except TypeError:
.... pass
.... return False
....
>>[cls for cls in dir(re) if isclass(getattr(re, cls))]
['Scanner', '_pattern_type', 'error']

Ian

Aug 15 '07 #5

P: n/a
Ian Clark wrote:
Torsten Bronger wrote:
>Hallöchen!

How can I get a list with all classes defined in the current module?
Thank you!

Tschö,
Torsten.

Assuming you want to see all classes in the re module:
>>import re
>>help(re) #best way
>>>
>>def isclass(cls):
... try:
... return issubclass(cls, cls)
... except TypeError:
... pass
... return False
...
>>[cls for cls in dir(re) if isclass(getattr(re, cls))]
['Scanner', '_pattern_type', 'error']

Ian
I love this list, I learn something new everyday. Didn't know about
inspect.isclass(). Ignore this post and look at Lawrence's.

Ian

Aug 15 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.