Message231996
Performing a logical negation (`not`) on `NotImplemented` should return `NotImplemented`. Currently, it returns `False`.
A common pattern for implementing __eq__ and __ne__ is to implement the comparison in __eq__, and simply delegate to it in __ne__ with a negation. However, if two values are incomparable, then __eq__ and __ne__ should both return NotImplemented. If you try to negate NotImplemented in __ne__, you will end up with a value of False, instead of NotImplemented, so you have to specifically test for this case.
For instance, here is how one would write the code now:
def __ne__(self, other):
eq = self.__eq__(other)
if eq is NotImplemented:
return NotImplemented
return not eq
Where as the following would be simpler, and could be used if this change was made:
def __ne__(self, other):
return not self.__eq__(other)
This is not simply sugar to reduce typing, it is safer because some coders may forget about NotImplemented and implement __ne__ as shown in the second example anyway, which is not actually correct with the current behavior. |
|
Date |
User |
Action |
Args |
2014-12-02 14:12:04 | Brian.Mearns | set | recipients:
+ Brian.Mearns |
2014-12-02 14:12:04 | Brian.Mearns | set | messageid: <1417529524.39.0.154038331271.issue22978@psf.upfronthosting.co.za> |
2014-12-02 14:12:04 | Brian.Mearns | link | issue22978 messages |
2014-12-02 14:12:04 | Brian.Mearns | create | |
|