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

Jeff Shannon jeff at ccvcorp.com
Thu Dec 9 12:39:58 EST 2004


Nick Coghlan wrote:

> Peter Hansen wrote:
>
>> Nick, could you please comment on why you say this about globals()?
>> I've never heard of any possibility of "unreliability" in updating
>> globals() and, as far as I know, a large body of code exists which
>> does in fact rely on this -- much of mine included. ;-)
>
>
> As Steve pointed out, I was, well, flat out wrong. You're quite 
> correct - it's only locals() that can cause a problem.


Of course, just because modifications of the dict returned by globals() 
*do* reliably result in modifications to the global namespace, doesn't 
mean it's a good idea. ;)

Personally, I don't *want* to create variables by "magic" like that.  
I'd rather pass a few extra parameters around, and explicitly create any 
global names I need.  If I really need to store objects under arbitrary 
names, then I probably should be using a regular dict (and passing it 
around as need be) instead of dumping stuff into globals().  (But then, 
I'm a devoted follower of the Pythonic principle of 'explicit is better 
than implicit' -- heck, even before I knew Python existed, I typically 
used this->name to reference C++ members despite the fact that 'this' 
was not required, just because I wanted to be able to *see* which 
variables were members and which weren't...)

Jeff Shannon
Technician/Programmer
Credit International




More information about the Python-list mailing list