Result of ``a is b''
Steve
dippyd at yahoo.com.au
Thu Mar 18 21:45:29 EST 2004
Asun Friere wrote:
> In fact __eq__ can be redefined even /so as to be an equivalence
> relation/ such that x==y => True and x is y => False. The
> 'fundamental properties' posisted, are simply not fundamental.
I think you have mis-read those fundamental properties.
They were:
(1) If x and y refer to the same object, "x is y"
yields True.
(2) If "x is y" yields True, "x==y" also yields True.
Equivalently, if "x==y" yields False, "x is y" also
yields False.
Property (2) assumes that __eq__ implements an
equivalence relation.
Notice that your objection that x==y returns True and x
is y returns False is *not* one of those properties. In
fact, that is the normal behaviour:
>>> a = [1, 2]
>>> b = [1] + [2]
>>> (a == b, a is b)
(1, 0)
I don't think the argument that one can redefine __eq__
to be something other than an equivalence relation is a
particularly strong argument. I would call that a bug,
even if it is a deliberate bug. If "==" doesn't do
equality, then it is an exceedingly bad piece of design.
So, assuming no bugs in the code, if a is not equal to
b then a is not the same object as b. On the other
hand, if a is equal to b, a may or may not be the same
object as b. And on the third hand, if a is the same
object as b, then a and b must be equal.
--
Steven D'Aprano
More information about the Python-list
mailing list