why () is () and [] is [] work in other way?

Chris Angelico rosuav at gmail.com
Sat Apr 21 11:03:15 EDT 2012


On Sat, Apr 21, 2012 at 10:51 PM, gst <g.starck at gmail.com> wrote:
> case 2) also ok to me:
>
>>>> x = id([]) ; y = id([])
>>>> x == y
> True
>>>>
>
>
> case 3) NOT ok to me :
>
>>>> x = id([])
>>>> y = id([])
>>>> x == y
> False
>>>>

The fact that ids get reused at all is an implementation detail ONLY.
In CPython, id() returns the object's memory address, but in other
Python implementations, it returns something else (one (Jython??)
returns a sequential number, never reused).

Here's my take on the difference here. You did all of these in a
single session, so by the time you got to case 3, x already contained
a value (the previous integer). The allocation and deallocation of
integer objects created your different behavior. I tried these from
command-line Python (3.2 on Windows, fwiw), restarting the interpreter
between, and got consistent results.

The main thing to know, though, is that the id MAY be reused once the
object is destroyed, but any reuse should be considered utterly
coincidental. A Python could be completely compliant with an id
implementation in which object type gets encoded into it somehow, but
programs written to make use of that information would be just as
vulnerable as those making use of reuse.

ChrisA



More information about the Python-list mailing list