469,288 Members | 2,357 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,288 developers. It's quick & easy.

getting dir(x), but not as list of strings?

mh
I want to iterate over members of a module, something like:

for i in dir(x):
if type(i) == types.FunctionType: ...

but of course dir() returns a list of strings. If x is a module,
how can I get the list of its members as their actual types?

Many TIA!
Mark

--
Mark Harrison
Pixar Animation Studios
Jun 27 '08 #1
4 993
On Wed, 21 May 2008 07:54:32 +0000, mh wrote:
I want to iterate over members of a module, something like:

for i in dir(x):
if type(i) == types.FunctionType: ...

but of course dir() returns a list of strings. If x is a module,
how can I get the list of its members as their actual types?
Take a look at the `inspect` module.

Ciao,
Marc 'BlackJack' Rintsch
Jun 27 '08 #2
Use

for i in dir(x):
print i # name of member as string
print getattr(x, i) # member

regards

Arno
On 21 Mai, 09:54, m...@pixar.com wrote:
I want to iterate over members of a module, something like:

for i in dir(x):
if type(i) == types.FunctionType: ...

but of course dir() returns a list of strings. If x is a module,
how can I get the list of its members as their actual types?

Many TIA!
Mark
Jun 27 '08 #3
mh@pixar.com wrote:
I want to iterate over members of a module, something like:

for i in dir(x):
if type(i) == types.FunctionType: ...

but of course dir() returns a list of strings. If x is a module,
how can I get the list of its members as their actual types?

Many TIA!
Mark

Use the builtin vars to get a dictionary of names and associated objects.

import sys
for name,ob in vars(sys).items():
print name,type(ob)

Gary Herron

setrecursionlimit <type 'builtin_function_or_method'>
getfilesystemencoding <type 'builtin_function_or_method'>
path_importer_cache <type 'dict'>
stdout <type 'file'>
version_info <type 'tuple'>
exc_clear <type 'builtin_function_or_method'>
prefix <type 'str'>
getrefcount <type 'builtin_function_or_method'>
byteorder <type 'str'>
....

Jun 27 '08 #4

"Gary Herron" <gh*****@islandtraining.comwrote in message
news:48**************@islandtraining.com...
| mh@pixar.com wrote:
| I want to iterate over members of a module, something like:
| >
| for i in dir(x):
| if type(i) == types.FunctionType: ...
| >
| but of course dir() returns a list of strings. If x is a module,
| how can I get the list of its members as their actual types?
| >
| Many TIA!
| Mark
| >
| >
| Use the builtin vars to get a dictionary of names and associated objects.
|
| import sys
| for name,ob in vars(sys).items():
| print name,type(ob)

This also works on classes, but apparently not on most other objects.

Jun 27 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Tetedeiench | last post: by
6 posts views Thread by Michael Hamm | last post: by
3 posts views Thread by dibblm | last post: by
5 posts views Thread by Amjad | last post: by
1 post views Thread by Scott | last post: by
2 posts views Thread by laredotornado | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.