Introspection at the module level?

Terry Reedy tjreedy at udel.edu
Sat Mar 6 12:07:29 EST 2004


"Roy Smith" <roy at panix.com> wrote in message
news:roy-AEE793.11085806032004 at reader2.panix.com...
> But I don't see how to get the values.  Getattr() is sort of what I
> want, but it works on objects, not modules.

Yes it does

>>> m = __import__(__name__)
>>> setattr(m, 'a', 3)
>>> dir(m)
['__builtins__', '__doc__', '__name__', 'a', 'm']
>>> getattr(m, 'a')
3

> I don't see how to get a handle to the module from inside the module,
i.e.
> there's no "self".

You have to define it yourself.  See above.

> What am I missing?
d={} # then, inside your loop:
 d[getattr(m,name)] = name #should do what you said you want

Terry J. Reedy







More information about the Python-list mailing list