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

Gary Herron gherron at islandtraining.com
Wed May 21 11:43:31 EDT 2008


mh at 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'>
...




More information about the Python-list mailing list