Why doesn't eval of generator expression work with locals?
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>
> Gabriel Genellina wrote:
>> En Tue, 27 Jan 2009 22:17:16 -0200, Robert Kern <robert.kern at gmail.com>
>> I *thought* I did understand this until I came to this example:
>>>>> id(globals()), id(locals())
>> (11239760, 11239760)
>> # ok, globals and locals are the same at the module level
>>>>> s = "(id(n) for n in [globals(),locals()])"
>> [11239760, 11239760] # still the same results
>>>>> s = "(id(n()) for n in [globals,locals])"
>> [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*...
More information about the Python-list