Primary
__eq__() ⚬|Documentation|1st|20260110211218-00-⌔
3. Data model — Python 3.14.2 documentation#object.lt
object.__lt__(self, other)
object.__le__(self, other)
object.__eq__(self, other)
object.__ne__(self, other)
object.__gt__(self, other)
object.__ge__(self, other)These are the so-called “rich comparison” methods. The correspondence between operator symbols and method names is as follows:
x<ycallsx.__lt__(y),x<=ycallsx.__le__(y),x==ycallsx.__eq__(y),x!=ycallsx.__ne__(y),x>ycallsx.__gt__(y), andx>=ycallsx.__ge__(y).A rich comparison method may return the singleton
NotImplementedif it does not implement the operation for a given pair of arguments. By convention,FalseandTrueare returned for a successful comparison. However, these methods can return any value, so if the comparison operator is used in a Boolean context (e.g., in the condition of anifstatement), Python will callbool()on the value to determine if the result is true or false.By default,
objectimplements__eq__()by usingis, returningNotImplementedin the case of a false comparison:True if x is y else NotImplemented. For__ne__(), by default it delegates to__eq__()and inverts the result unless it isNotImplemented. There are no other implied relationships among the comparison operators or default implementations; for example, the truth of(x<y or x==y)does not implyx<=y. To automatically generate ordering operations from a single root operation, seefunctools.total_ordering().By default, the
objectclass provides implementations consistent with Value comparisons: equality compares according to object identity, and order comparisons raiseTypeError. Each default method may generate these results directly, but may also returnNotImplemented.See the paragraph on
__hash__()for some important notes on creating hashable objects which support custom comparison operations and are usable as dictionary keys.There are no swapped-argument versions of these methods (to be used when the left argument does not support the operation but the right argument does); rather,
__lt__()and__gt__()are each other’s reflection,__le__()and__ge__()are each other’s reflection, and__eq__()and__ne__()are their own reflection. If the operands are of different types, and the right operand’s type is a direct or indirect subclass of the left operand’s type, the reflected method of the right operand has priority, otherwise the left operand’s method has priority. Virtual subclassing is not considered.When no appropriate method returns any value other than
NotImplemented, the==and!=operators will fall back toisandis not, respectively.Printed 2026-06-28.
(echo:: @ ᯤ)
Link to original
Secondary
• • •