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

Scott David Daniels Scott.Daniels at Acm.Org
Wed Jan 28 19:00:43 CET 2009


Gabriel Genellina wrote:
> 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)
This is a bad test.  We know distinct objects are distinct, but:
 >>> print id(object()), id(object())
10598120 10598120

 >>> a = object(); b = object()
 >>> print id(a), id(b)
10598120 10598128

The reason is that once your created object has its id taken, you
must keep a handle on it, otherwise it may get recycled and reused.

That is,  id(var1) == id(var2)  implies   var1 is var2,
but      id(expr1) == id(expr2)  does not even imply  expr1 == expr2

--Scott David Daniels
Scott.Daniels at Acm.Org



More information about the Python-list mailing list