Usually the order of operands of the == operator does not matter. bool(a
== b) should return the same as bool(b == a). Correct __eq__ should look
like:
def __eq__(self, other):
if not know how to compare with other:
return NotImplemented
return the result of comparison
But we work with non-perfect code written by non-perfect people.
__eq__() can return False instead of NotImplemented for comparison with
different type (it is not the worst case, in worst case it raises
AttributeError or TypeError). So the order of operands can matter.
See https://bugs.python.org/issue37555 as an example of a real world issue.
The typical implementation of the __contains__ method looks like:
def __contains__(self, needle):
for item in self:
if item == needle: # or needle == item
return True
return False
The question is where the needle should be: at the right or at the left
side of ==?
In __contains__ implementations in list, tuple and general iterators
(see PySequence_Contains) the needle is at the right side. But in
count(), index() and remove() it is at the left side. In array it is
effectively always at the left side since its __eq__ is not invoked.
The question is whether we should unify implementations and always use
the needle at some particular side and what this side should be.
_______________________________________________
Python-Dev mailing list -- python-dev@python.org
To unsubscribe send an email to python-dev-le...@python.org
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at
https://mail.python.org/archives/list/python-dev@python.org/message/VSV4K4AOKM4CBQMOELPFV5VMYALPH464/