Introspection at the module level?

Sean Ross sross at connectmail.carleton.ca
Sat Mar 6 11:45:32 EST 2004


"Roy Smith" <roy at panix.com> wrote in message
news:roy-AEE793.11085806032004 at reader2.panix.com...
> I've got a module that defines a bunch of constants:
>
> OP_FOO = 1
> OP_BAR = 2
> OP_BAZ = 37
>
> and so on.  The values are all unique.  I want to build, at module
> import time, a reverse map of these constants, i.e. I want to end up
> with:
>
> {1: "OP_FOO", 2: "OP_BAR", 37: "OP_BAZ"}
>

I'm not sure how to do this "at module import time", but if you just
want to build a reverse dictionary of globals variables that start with
"OP_", you can try this:

rd = dict([(v,k) for k,v in globals().copy().iteritems() if
k.startswith("OP_")])


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

http://groups.google.ca/groups?hl=en&lr=&ie=UTF-8&oe=UTF-8&safe=off&selm=giGhb.262770%24R32.8518969%40news2.tin.it&rnum=6






More information about the Python-list mailing list