diff -r 58c90cf81293 Doc/reference/expressions.rst --- a/Doc/reference/expressions.rst Fri Oct 05 14:10:57 2012 +0300 +++ b/Doc/reference/expressions.rst Fri Oct 05 14:13:46 2012 -0700 @@ -1041,58 +1041,17 @@ *c*, so that, e.g., ``x < y > z`` is perfectly legal (though perhaps not pretty). -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`. - -Comparison of objects of the same type depends on the type: - -* Numbers are compared arithmetically. - -* The values :const:`float('NaN')` and :const:`Decimal('NaN')` are special. - The are identical to themselves, ``x is x`` but are not equal to themselves, - ``x != x``. Additionally, comparing any value to a not-a-number value - will return ``False``. For example, both ``3 < float('NaN')`` and - ``float('NaN') < 3`` will return ``False``. - -* Bytes objects are compared lexicographically using the numeric values of their - elements. - -* Strings are compared lexicographically using the numeric equivalents (the - result of the built-in function :func:`ord`) of their characters. [#]_ String - and bytes object can't be compared! - -* Tuples and lists are compared lexicographically using comparison of - corresponding elements. This means that to compare equal, each element must - compare equal and the two sequences must be of the same type and have the same - length. - - If not equal, the sequences are ordered the same as their first differing - elements. For example, ``[1,2,x] <= [1,2,y]`` has the same value as - ``x <= y``. If the corresponding element does not exist, the shorter - sequence is ordered first (for example, ``[1,2] < [1,2,3]``). - -* Mappings (dictionaries) compare equal if and only if they have the same - ``(key, value)`` pairs. Order comparisons ``('<', '<=', '>=', '>')`` - raise :exc:`TypeError`. - -* Sets and frozensets define comparison operators to mean subset and superset - tests. Those relations do not define total orderings (the two sets ``{1,2}`` - and {2,3} are not equal, nor subsets of one another, nor supersets of one - another). Accordingly, sets are not appropriate arguments for functions - which depend on total ordering. For example, :func:`min`, :func:`max`, and - :func:`sorted` produce undefined results given a list of sets as inputs. - -* Most other objects of built-in types compare unequal unless they are the same - object; the choice whether one object is considered smaller or larger than - another one is made arbitrarily but consistently within one execution of a - program. +The operators ``<``, ``>``, ``==``, ``>=``, ``<=``, and ``!=`` compare the +values of two objects. The objects need not be of the same type. With +``==`` and ``!=``, an object is equal to itself and different objects +(a is not b) compare unequal, unless the class of the first define a +specific :meth:`__eq__()` and :meth:`__ne__()`. With ``<``, ``>``, +``<=``, and ``>=``, comparison raises a :exc:`TypeError`, unless the class +of the first object defines specific :meth:`__lt__()`, :meth:`__gt__()`, +:meth:`__le__()`, and :meth:`__ge__()`, or the class of the second defines +the reflected method (:meth:`__ge__()` reflects :meth:`__lt__()`, etcetera. +Built-in numbers compare as expected, even when of different types. +Built-in sequences compare lexicographically. Comparison of objects of the differing types depends on whether either of the types provide explicit support for the comparison. Most numeric types