Why doesn't eval of generator expression work with locals?

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Wed Jan 28 16:43:43 CET 2009


En Wed, 28 Jan 2009 01:36:57 -0200, Steve Holden <steve at holdenweb.com>  
escribió:
> Gabriel Genellina wrote:
>> En Tue, 27 Jan 2009 22:17:16 -0200, Robert Kern <robert.kern at gmail.com>
>> escribió:

>> I *thought* I did understand this until I came to this example:
>>
>> 1)
>>>>> id(globals()), id(locals())
>> (11239760, 11239760)
>>
>> # ok, globals and locals are the same at the module level
>>
>> 2)
>>>>> s = "(id(n) for n in [globals(),locals()])"
>>>>> list(eval(s))
>> [11239760, 11239760]  # still the same results
>>
>> 3)
>>>>> s = "(id(n()) for n in [globals,locals])"
>>>>> list(eval(s))
>> [11239760, 12583248]  # locals() is different
>>
> No, locals is different, not locals(). You are looking at two different
> functions that return the same object when called in the given context,
> that's all.

Perhaps you didn't notice that I shifted the () from right to left. I'm  
always printing the result of *calling* globals and locals -- the only  
change is *where* I do call them.

>>>>> [id(n) for n in [globals(),locals()]]
>> [11239760, 11239760]
>>>>> [id(n()) for n in [globals,locals]]
>> [11239760, 12583248]
>>
>> Seems that it is important *when* those functions are evaluated, but I
>> don't understand *why*...

-- 
Gabriel Genellina




More information about the Python-list mailing list