All names in the current module

T

Torsten Bronger

Hallöchen!

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

Tschö,
Torsten.
 
L

Lawrence Oluyede

Torsten Bronger said:
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.['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:
.... print member, inspect.isclass(getattr(t, member))
....
A True
__builtins__ False
__doc__ False
__file__ False
__name__ False

HTH
 
F

Fabio Z Tessitore

Il Wed, 15 Aug 2007 19:01:17 +0200, Lawrence Oluyede ha scritto:
Torsten Bronger said:
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.['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
 
L

Lawrence Oluyede

Fabio Z Tessitore said:
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.
 
I

Ian Clark

Torsten said:
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:
.... 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

Ian Clark

Ian said:
Torsten said:
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:
... 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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,280
Latest member
BGBBrock56

Latest Threads

Top