The __ne__ method is the special method Python calls when you evaluate a != b. In Python 3 you almost never need to write it: if you define __eq__, the default __ne__ calls it and inverts the result, unless __eq__ returns NotImplemented. Write your own __ne__ only when inequality must follow different rules from equality.
This is a change from Python 2, where != and == were independent and you had to define both. Many older tutorials still show a hand-written __ne__ that simply negates __eq__, which is redundant today.
__ne__ does and when Python calls it__ne__ in Python 3__ne__ safely
__ne__ does and when Python calls itWhen Python evaluates a != b, it calls type(a).__ne__(a, b). If that returns NotImplemented, Python tries the reflected call type(b).__ne__(b, a). If both return NotImplemented, the comparison falls back to identity: two objects are unequal unless they are the same object.
The signature is always the same.
self is the instance on the left of !=.other is the object on the right. It can be any type, so never assume it is an instance of your class.bool, or NotImplemented when the operand type is not supported.__ne__ in Python 3The base object.__ne__ delegates to __eq__ and inverts the result, unless that result is NotImplemented. That means this class already has a correct !=.
The last line shows why returning NotImplemented matters. Both sides decline the comparison, Python falls back to identity, and the objects compare as unequal instead of raising an AttributeError.
__ne__ safelyIf you do override __ne__, handle foreign types the same way. The original pattern of reading other.x directly fails as soon as someone compares your object with a string or None.
This __ne__ is equivalent to the default one, so in real code you would delete it. The example only shows the shape of a correct override: check the type, return NotImplemented for anything unknown, and keep it consistent with __eq__.
A custom __ne__ is justified when inequality is not the plain negation of equality. A typical case is a three-valued, SQL-style NULL where comparing with an unknown value is neither equal nor unequal.
Here both == and != are false when a value is unknown. The default __ne__ could not express that, because it would return True for the second line. Be aware that such semantics surprise other developers, so document them.
For plain data holders, let the standard library generate the comparison methods. A dataclass creates __eq__ from its fields, and != works through the default __ne__.
With frozen=True and the default eq=True, the dataclass also generates a matching __hash__, so instances can be used in sets and as dictionary keys.
__eq__ first and let __ne__ follow it.NotImplemented, not False, for operand types you do not support.__eq__, define __hash__ too, or the class becomes unhashable because Python sets __hash__ to None.__ne__ in Python 3?No. The default implementation inverts __eq__, so defining __eq__ is enough in almost every case.
__ne__ and __eq__?__eq__ implements == and __ne__ implements !=. In Python 3 the second is derived from the first unless you override it.
__ne__ return NotImplemented?It tells Python that your class cannot compare with that type, so Python tries the other operand and then falls back to identity. Returning False or raising an error breaks that protocol.
functools.total_ordering provide __ne__?No. It fills in the ordering methods __lt__, __le__, __gt__ and __ge__ from one of them plus __eq__. Inequality still comes from __eq__.
__ne__ powers the != operator. Python 3 derives it from __eq__ automatically, so a correct __eq__ and __hash__ is normally all you need, or a dataclass that generates them for you. Override __ne__ only for genuinely different inequality semantics, and always return NotImplemented for unsupported operand types.