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

John O'Hagan research at johnohagan.com
Sat Apr 21 00:04:03 EDT 2012


On Sat, 21 Apr 2012 04:25:36 +0100
Rotwang <sg552 at hotmail.co.uk> wrote:

> On 21/04/2012 01:01, Roy Smith wrote:
> > In article<877gxajit0.fsf at dpt-info.u-strasbg.fr>,
> >   Alain Ketterlin<alain at dpt-info.u-strasbg.fr>  wrote:
> >
> >> Tuples are immutable, while lists are not.
> >
> > If you really want to have fun, consider this classic paradox:
> >
> >>>> [] is []
> > False
> >>>> id([]) == id([])
> > True
> 
> Huh. This is not what I would have expected. What gives?
> 

I'm just guessing here, but as id is "is guaranteed to be unique among
simultaneously existing objects", the list literals on each side of the
comparison do not simultaneously exist; presumably the first one is created,
id'ed, then garbage collected (as it is not named), then the other one is
created and gets the same id just freed up by the first one.

OTOH, "is" hangs on to both objects in order to compare them.

c.f.:

>>> l, m = [], []
>>> id(l) == id(m)
False
  

Is that right?

--
John 



More information about the Python-list mailing list