473,396 Members | 1,774 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

All names in the current module

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
5 1460
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Ron_Adam | last post by:
Hi, Sometimes it just helps to see what's going on, so I've been trying to write a tool to examine what names are pointing to what objects in the current scope. This still has some glitches,...
6
by: Mayer | last post by:
Hello: Is there a way to see at the python prompt the names of all the public methods of a class or the names exported by a module? I know that GUI-based IDEs have a nifty way of displaying...
1
by: Larry Bird | last post by:
I've created a AlertDataClass below within the class I have tables and column that I've create. In the AlertDataAccess class I'm trying to insert data into my tables. AlertDataAccess is a Module...
5
by: Larry Bird | last post by:
I've created a AlertDataClass below within the class I have tables and column that I've create. In the AlertDataAccess class I'm trying to insert data into my tables. AlertDataAccess is a Module...
66
by: Mike Meyer | last post by:
It seems that the distinction between tuples and lists has slowly been fading away. What we call "tuple unpacking" works fine with lists on either side of the assignment, and iterators on the...
0
by: Mythran | last post by:
I wrote some code that is supposed to enumerate through the specified file's win32 resources and return a string-array of all icon names. When it runs, it returns a string-array with a bunch of...
7
by: Petr Jakes | last post by:
I have got names of functions stored in the file. For the simplicity expect one row only with two function names: printFoo, printFOO In my code I would like to define functions and then to read...
34
by: davehowey | last post by:
I have a problem. I'm writing a simulation program with a number of mechanical components represented as objects. When I create instances of objects, I need to reference (link) each object to the...
6
by: Kay Schluehr | last post by:
set, int, float, list, object,... Don't see any of the basic types following the capitalized word convention for classes covered by PEP 08. This does not hold only for __builtins__ in the strict...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.