cpython: 294b8a7957e9 (original) (raw)

--- a/Doc/reference/expressions.rst +++ b/Doc/reference/expressions.rst @@ -1036,10 +1036,6 @@ must be integers. .. _comparisons: -.. _is: -.. _is not: -.. _in: -.. _not in: Comparisons =========== @@ -1075,66 +1071,183 @@ Note that a op1 b op2 c doesn't impl c, so that, e.g., x < y > z is perfectly legal (though perhaps not pretty). +Value comparisons +----------------- + The operators <, >, ==, >=, <=, and != compare the -values of two objects. The objects need not have the same type. If both are -numbers, they are converted to a common type. Otherwise, the == and != -operators always* consider objects of different types to be unequal, while the -<, >, >= and <= operators raise a :exc:TypeError when -comparing objects of different types that do not implement these operators for -the given pair of types. You can control comparison behavior of objects of -non-built-in types by defining rich comparison methods like :meth:__gt__, -described in section :ref:customization. +values of two objects. The objects do not need to have the same type. + +Chapter :ref:objects states that objects have a value (in addition to type +and identity). The value of an object is a rather abstract notion in Python: +For example, there is no canonical access method for an object's value. Also, +there is no requirement that the value of an object should be constructed in a +particular way, e.g. comprised of all its data attributes. Comparison operators +implement a particular notion of what the value of an object is. One can think +of them as defining the value of an object indirectly, by means of their +comparison implementation. + +Because all types are (direct or indirect) subtypes of :class:object, they +inherit the default comparison behavior from :class:object. Types can +customize their comparison behavior by implementing +:dfn:rich comparison methods like :meth:__lt__, described in +:ref:customization. + +The default behavior for equality comparison (== and !=) is based on +the identity of the objects. Hence, equality comparison of instances with the +same identity results in equality, and equality comparison of instances with +different identities results in inequality. A motivation for this default +behavior is the desire that all objects should be reflexive (i.e. x is y +implies x == y). -Comparison of objects of the same type depends on the type: +A default order comparison (<, >, <=, and >=) is not provided; +an attempt raises :exc:TypeError. A motivation for this default behavior is +the lack of a similar invariant as for equality. - Numbers are compared arithmetically. +The behavior of the default equality comparison, that instances with different +identities are always unequal, may be in contrast to what types will need that +have a sensible definition of object value and value-based equality. Such +types will need to customize their comparison behavior, and in fact, a number +of built-in types have done that. -* The values :const:float('NaN') and :const:Decimal('NaN') are special.

+

+ +* Mappings (instances of :class:dict) compare equal if and only if they have

+ +* Comparison should be symmetric.

+

+

-* Most other objects of built-in types compare unequal unless they are the same

+ +* Comparison should be transitive.

+

+ +* Inverse comparison should result in the boolean negation.

+

-Comparison of objects of differing types depends on whether either of the -types provide explicit support for the comparison. Most numeric types can be -compared with one another. When cross-type comparison is not supported, the -comparison method returns NotImplemented.

+

.. [#] Due to automatic garbage-collection, free lists, and the dynamic nature of descriptors, you may notice seemingly unusual behaviour in certain uses of

--- a/Misc/NEWS +++ b/Misc/NEWS @@ -224,6 +224,13 @@ Library Documentation ------------- +- Issue #12067: Rewrite Comparisons section in the Expressions chapter of the