Adding to a module's __dict__?

Gregory Ewing greg.ewing at canterbury.ac.nz
Thu Mar 4 05:43:20 EST 2010


Roy Smith wrote:
> The idea is I want to put in the beginning of the module:
> 
> declare('XYZ_FOO', 0, "The foo property")
> declare('XYZ_BAR', 1, "The bar property")
> declare('XYZ_BAZ', 2, "reserved for future use")

Okay, that seems like a passable excuse.

One thing to watch out for is that if your 'declare' function
is defined in a different module, when it calls globals() it
will get the globals of the module it's defined in, not the
one it's being called from.

There's a hackish way of getting around that, but it might
be better to put all of these symbols in a module of their
own and import them from it. The declare() function can then
be defined in that same module so that it accesses the right
globals.

-- 
Greg



More information about the Python-list mailing list