How do I do this? (eval() on the left hand side)

Jeff Shannon jeff at
Thu Dec 9 21:38:09 CET 2004

Peter Hansen wrote:

> The main way I use this is in some sort of a "const" module, which
> provides access to a large set of constant information.  In other
> words, in contrast to what you had in mind when you wrote the
> above, I'm dealing with neither "variables" nor information that
> _would_ best be put in a dict.
> [...]
> In general I would say that mucking with globals() like this is
> probably best restricted to constants like in this case, if at all.

Indeed, for almost every rule, there are exceptions..  Legitimate cases 
also exist for using eval() and exec, though in general both of those 
are better avoided.  The case you describe does sound like a legitimate 
use of writing to globals(), with realistic expectations of the costs 
and benefits

(Actually, in your provided example, I might have parse the 
header file into a dict named const, and then have my modules use 'from 
const import const'.  But I will admit that the extra complication on 
import may not be worth the "cleaner" module internals...  As always, 
it's a tradeoff, and each programmer must decide which costs are worth 
bearing in their particular situation.)

Jeff Shannon
Credit International

More information about the Python-list mailing list