Re: Help identifying

tnb2d@henson.cs.virginia.edu
Sun, 8 May 94 13:12:05 EDT

Tim Peters writes:

| A more general trick is to use 'eval' to map a variable's name (a string)
| to the variable's current binding. E.g.,
|
| module_type = type(__main__)
| for n in dir(__main__ ):
| value = eval(n)
| if type(value) is module_type:
| print n, dir(value)
|
| Or if you wanted to do dir() on *everything* with a __dict__,
|
| for n in dir(__main__):
| value = eval(n)
| if hasattr(value, '__dict__'):
| print n, dir(value)
|

Ah, but Tim! You've forgotten to take advantage of one of our
new (v1.0.2) features! Who needs eval when you have vars?

>>> import __main__
>>> module_type = type(__main__)
>>> v = vars(__main__)
>>> for n in v.keys():
... if type(v[n]) is module_type:
... print n+':', dir(v[n])
...
__main__: ['__main__', '__name__', 'module_type', 'n', 'v']

and the second (anything with a '__dict__') example would be:

>>> for n in v.keys():
... if hasattr(v[n], '__dict__'):
... print n+':', dir(v[n])
...
__main__: ['__main__', '__name__', 'module_type', 'n', 'v']

-------> Tommy.

"Subvert the parental paradigm - Refuse Birth."