diff --git a/Doc/reference/expressions.rst b/Doc/reference/expressions.rst --- a/Doc/reference/expressions.rst +++ b/Doc/reference/expressions.rst @@ -1023,10 +1023,6 @@ .. _comparisons: -.. _is: -.. _is not: -.. _in: -.. _not in: Comparisons =========== @@ -1074,8 +1070,9 @@ * Numbers of built-in types (:ref:`typesnumeric`) and of standard library types (:mod:`fractions` and :mod:`decimal`) compare mathematically correct. Complex - numbers are not considered orderable; order comparisons ``('<', '<=', '>=', '>')`` - on complex numbers raise :exc:`TypeError`. + numbers are not considered orderable; using order comparisons + (``<``, ``<=``, ``>=``, ``>``) on complex numbers raises :exc:`TypeError`. + 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 @@ -1083,29 +1080,29 @@ ``float('NaN') < 3`` will return ``False``. * Bytes objects are compared lexicographically using the numeric values of their - bytes. + bytes elements. * Strings are compared lexicographically using the numeric equivalents (the result of the built-in function :func:`ord`) of their characters. [#]_ String objects cannot be compared with bytes objects, and vice versa! -* Tuples, lists and ranges are compared by comparing corresponding elements. - This means that for two sequences to compare equal, they must be of the same - type, have the same length, and each pair of corresponding elements must - compare equal. +* Tuples, lists and ranges are compared by comparing elements at corresponding + positions in the sequence. This means that for two sequences to compare equal, + they must be of the same type, have the same length, and each pair of + corresponding elements must compare equal. 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 + elements. For example, ``[1, 2, x] <= [1, 2, y]`` has the same value as ``x <= y``. If a corresponding element does not exist, the shorter - sequence is ordered first (for example, ``[1,2] < [1,2,3]``). + 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`. +* Mappings (dictionaries) compare equal if and only if the values at matching + keys compare equal. Keys match if they compare equal. Using order comparisons + (``<``, ``<=``, ``>=``, ``>``) on mappings raises :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 + 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. @@ -1119,16 +1116,21 @@ * Numbers of built-in types (:ref:`typesnumeric`) and of standard library types (:mod:`fractions` and :mod:`decimal`) compare between different types as - described for same types. + described for same types. Any type conversions that are performed will be + mathematically correct (i.e. without loosing precision). * Any other built-in types do not support comparison between different types. Comparisons raise :exc:`TypeError`. Comparison of objects involving user-defined classes depends on whether either of -the participating types supports the comparison operation. Coomparison operations -can be implemented by defining rich comparison methods like :meth:`__gt__`, -described in section :ref:`customization`. +the participating types supports the comparison operation. Comparison operations +can be implemented by defining :dfn:`rich comparison methods` like :meth:`__lt__`, +described in :ref:`customization`. +.. _is: +.. _is not: +.. _in: +.. _not in: .. _membership-test-details: .. _membership-test-operators: