Semantics of ==

Terry Reedy tjreedy at udel.edu
Tue Mar 16 21:33:24 EST 2004


"Axel Boldt" <axelboldt at yahoo.com> wrote in message
news:40200384.0403161538.7407d9e2 at posting.google.com...
> Still trying to understand "=="...

means equal in value as defined be Python and user code

> It appears as if two equal objects
> can become unequal if you perform the same operations on them:
>
>   >>> 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.

This is routine:
>>> print 0, 0.0, 0==0.0
0 0.0 1
>>> 1.0 == .999999999999999999999999999999999999
1

Equal in value != equal in internal structure.

>   >>> s[0]=2
>   >>> w[0]=2
>   >>> s==w
>   False
>
> All of a sudden they have become unequal.

1 == 1.0 but type(1) != type(1.0).  Okay, that's cheating, so

>>> 1|1
1
>>> 1.0|1
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: unsupported operand type(s) for |: 'float' and 'int'

Doing same thing to equals has different results.  Or

>>> a=12345678909876
>>> b=float(a)
>>> a*a, b*b
(152415787745757059730335376L, 1.5241578774575706e+026)

now unequal to human eye, but

>>> a*a == b*b
1

due to conversion of a*a to float(a*a) for comparison.

Terry J. Reedy







More information about the Python-list mailing list