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

M

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
 
M

Marc 'BlackJack' Rintsch

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
 
A

akeppke

Use

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

regards

Arno
 
G

Gary Herron

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'>
....
 
T

Terry Reedy

| (e-mail address removed) 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.
 

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

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,071
Latest member
MetabolicSolutionsKeto

Latest Threads

Top