472,958 Members | 2,364 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,958 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 1436
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...
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

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.