Anonymus functions revisited

Duncan Booth duncan.booth at invalid.invalid
Thu Mar 24 04:20:52 EST 2005


Ron wrote:

>>> A working makeVars seems not to be different from
>>> 
>>> def makeVars(**nameVals):
>>>    globals().update(nameVals)
>>
>>Not quite. If Ron can come up with a working makeVars it would update
>>the caller's globals whereas what you just posted updates makeVar's
>>globals so there is a difference (when the makeVars and the calling
>>function are in different modules), just not a very useful one.
> 
> How about this one?   The only reliable way I found to do it is to
> pass locals() to the function.

Yes, but you are still missing the fundamental point. The locals() 
dictionary is not guaranteed to do anything useful if you update it. The 
current C implementation will reflect changes in the locals dictionary if 
you call locals() from global scope or in a few other circumstances, but 
this is simply an implementation detail.

If you want to update global variables then use globals() or setattr on the 
module. Only use locals() to access local variables indirectly, never to 
try and set them.



More information about the Python-list mailing list