Introspection at the module level?

Sean Ross sross at
Sat Mar 6 17:45:32 CET 2004

"Roy Smith" <roy at> wrote in message
news:roy-AEE793.11085806032004 at
> 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

>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".

More information about the Python-list mailing list