Message76270
3.0c3 doc (Basic customization) says
"There are no implied relationships among the comparison operators. The
truth of x==y does not imply that x!=y is false. Accordingly, when
defining __eq__(), one should also define __ne__() so that the operators
will behave as expected. "
In http://mail.python.org/pipermail/python-ideas/2008-October/002235.html
Guido says
"I should also note that part of George's proposal has already been
implemented: if you define __eq__, you get a complementary __ne__ for
free. However it doesn't work the other way around (defining __ne__
doesn't give you __eq__ for free), and there is no similar
relationship for the ordering operators."
And indeed, as Arnaud Delobelle posted on python-list
class A:
def __init__(self, x):
self.x = x
def __eq__(self, other):
return self.x == other.x
a, b, c = A(1), A(1), A(2)
print(a==b, b==c, c==a) # True, False, False
print(a!=b, b!=c, c!=a) # False, True, True
Suggested revision:
"There is one implied relationship among comparison operators: defining
__eq__ gives an automatic __ne__ (but not the other way). There is no
similar relationship for the order comparisons. |
|
Date |
User |
Action |
Args |
2008-11-23 18:21:34 | terry.reedy | set | recipients:
+ terry.reedy, georg.brandl |
2008-11-23 18:21:33 | terry.reedy | set | messageid: <1227464493.77.0.130820783094.issue4395@psf.upfronthosting.co.za> |
2008-11-23 18:21:32 | terry.reedy | link | issue4395 messages |
2008-11-23 18:21:31 | terry.reedy | create | |
|