Semantics of ==

Raymond Hettinger python at rcn.com
Thu Mar 25 22:36:13 EST 2004


[Axel Boldt]
> >   >>> l=[1]
> >   >>> s=l
> >   >>> l.append(s)
> >   >>> w=[1]
> >   >>> r=[1,w]
> >   >>> w.append(r)
> >   >>> s
>  [1, [...]]
> >   >>> w
>  [1, [1, [...]]]
> >   >>> s==w
> >   True
> >
> > Note that they're equal, yet are printed differently.
> >
> >   >>> s[0]=2
> >   >>> w[0]=2
> >   >>> s==w
> >   False
> >
> > All of a sudden they have become unequal.

[John Roth]
> You've got a recursive structure! I originally thought
> that the [...] was something you'd done to the printout,
> but it isn't.
> 
> I think the original True is a bug. It's getting confused
> by the recursion.

Armin Rigo found and fixed this for Py2.4:

Python 2.4a0 (#46, Mar 23 2004, 01:55:44) [MSC v.1200 32 bit (Intel)] on win32
 <snipped>
>>> s
[1, [...]]
>>> w
[1, [1, [...]]]
>>> s==w


Traceback (most recent call last):
  File "<pyshell#9>", line 1, in -toplevel-
    s==w
RuntimeError: maximum recursion depth exceeded in cmp


Raymond Hettinger



More information about the Python-list mailing list