Introspection at the module level?

Roy Smith roy at panix.com
Sun Mar 7 10:41:04 EST 2004


In article <mailman.98.1078668652.19534.python-list at python.org>,
 Skip Montanaro <skip at pobox.com> wrote:

> Have a look at
> 
>     http://manatee.mojam.com/~skip/python/ConstantMap.py

This pointed me to the simpliest solution.  I ended up with:

map = {}
for name, value in globals().items():
        if name.startswith ("OP_"):
                map[value] = name

which seems to work just fine.  Thanks to everybody who responded with 
suggestions.

The danger here is that I'm depending on the fact that there are no 
globals defined elsewhere in the system which start with "OP_", but that 
seems like a pretty minimal risk.  In fact, it looks like all the 
externally defined globals are of the form __xxx__.



More information about the Python-list mailing list