cpython: 8a9904c5cb1d (original) (raw)

--- a/Doc/reference/expressions.rst +++ b/Doc/reference/expressions.rst @@ -1058,10 +1058,6 @@ must be plain or long integers. The arg .. _comparisons: -.. _is: -.. _is not: -.. _in: -.. _not in: Comparisons =========== @@ -1101,39 +1097,98 @@ The forms <> and != are equivale preferred; where != is mentioned below <> is also accepted. The <> spelling is considered obsolescent. +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, objects of different -types always* compare unequal, and are ordered consistently but arbitrarily. -You can control comparison behavior of objects of non-built-in types by defining -a __cmp__ method or rich comparison methods like __gt__, described in -section :ref:specialnames. +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. + +Types can customize their comparison behavior by implementing +a :meth:__cmp__ method or +: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). + +The default order comparison (<, >, <=, and >=) gives a +consistent but arbitrary order. (This unusual definition of comparison was used to simplify the definition of operations like sorting and the :keyword:in and :keyword:not in operators. In the future, the comparison rules for objects of different types are likely to change.) -Comparison of objects of the same type depends on the type: +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. - Numbers are compared arithmetically. +The following list describes the comparison behavior of the most important +built-in types. -* Strings are compared lexicographically using the numeric equivalents (the

-* Mappings (dictionaries) compare equal if and only if their sorted (key, value)

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

+ +* Comparison should be symmetric.

+

+

+

+ +* Comparison should be transitive.

+

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

+

+

+

-.. [#] The implementation computes this efficiently, without constructing lists or

.. [#] Earlier versions of Python used lexicographic comparison of the sorted (key, value) lists, but this was very expensive for the common case of comparing for

--- a/Misc/NEWS +++ b/Misc/NEWS @@ -73,6 +73,14 @@ C API