Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e52c41a

Browse files
committed
Issue #12067: Merge comparisons doc from 3.4 into 3.5
2 parents 1ccf44c + aa0da86 commit e52c41a

2 files changed

Lines changed: 195 additions & 56 deletions

File tree

Doc/reference/expressions.rst

Lines changed: 188 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1036,10 +1036,6 @@ must be integers.
10361036

10371037

10381038
.. _comparisons:
1039-
.. _is:
1040-
.. _is not:
1041-
.. _in:
1042-
.. _not in:
10431039

10441040
Comparisons
10451041
===========
@@ -1075,66 +1071,183 @@ Note that ``a op1 b op2 c`` doesn't imply any kind of comparison between *a* and
10751071
*c*, so that, e.g., ``x < y > z`` is perfectly legal (though perhaps not
10761072
pretty).
10771073

1074+
Value comparisons
1075+
-----------------
1076+
10781077
The operators ``<``, ``>``, ``==``, ``>=``, ``<=``, and ``!=`` compare the
1079-
values of two objects. The objects need not have the same type. If both are
1080-
numbers, they are converted to a common type. Otherwise, the ``==`` and ``!=``
1081-
operators *always* consider objects of different types to be unequal, while the
1082-
``<``, ``>``, ``>=`` and ``<=`` operators raise a :exc:`TypeError` when
1083-
comparing objects of different types that do not implement these operators for
1084-
the given pair of types. You can control comparison behavior of objects of
1085-
non-built-in types by defining rich comparison methods like :meth:`__gt__`,
1086-
described in section :ref:`customization`.
1087-
1088-
Comparison of objects of the same type depends on the type:
1089-
1090-
* Numbers are compared arithmetically.
1091-
1092-
* The values :const:`float('NaN')` and :const:`Decimal('NaN')` are special.
1093-
They are identical to themselves, ``x is x`` but are not equal to themselves,
1094-
``x != x``. Additionally, comparing any value to a not-a-number value
1078+
values of two objects. The objects do not need to have the same type.
1079+
1080+
Chapter :ref:`objects` states that objects have a value (in addition to type
1081+
and identity). The value of an object is a rather abstract notion in Python:
1082+
For example, there is no canonical access method for an object's value. Also,
1083+
there is no requirement that the value of an object should be constructed in a
1084+
particular way, e.g. comprised of all its data attributes. Comparison operators
1085+
implement a particular notion of what the value of an object is. One can think
1086+
of them as defining the value of an object indirectly, by means of their
1087+
comparison implementation.
1088+
1089+
Because all types are (direct or indirect) subtypes of :class:`object`, they
1090+
inherit the default comparison behavior from :class:`object`. Types can
1091+
customize their comparison behavior by implementing
1092+
:dfn:`rich comparison methods` like :meth:`__lt__`, described in
1093+
:ref:`customization`.
1094+
1095+
The default behavior for equality comparison (``==`` and ``!=``) is based on
1096+
the identity of the objects. Hence, equality comparison of instances with the
1097+
same identity results in equality, and equality comparison of instances with
1098+
different identities results in inequality. A motivation for this default
1099+
behavior is the desire that all objects should be reflexive (i.e. ``x is y``
1100+
implies ``x == y``).
1101+
1102+
A default order comparison (``<``, ``>``, ``<=``, and ``>=``) is not provided;
1103+
an attempt raises :exc:`TypeError`. A motivation for this default behavior is
1104+
the lack of a similar invariant as for equality.
1105+
1106+
The behavior of the default equality comparison, that instances with different
1107+
identities are always unequal, may be in contrast to what types will need that
1108+
have a sensible definition of object value and value-based equality. Such
1109+
types will need to customize their comparison behavior, and in fact, a number
1110+
of built-in types have done that.
1111+
1112+
The following list describes the comparison behavior of the most important
1113+
built-in types.
1114+
1115+
* Numbers of built-in numeric types (:ref:`typesnumeric`) and of the standard
1116+
library types :class:`fractions.Fraction` and :class:`decimal.Decimal` can be
1117+
compared within and across their types, with the restriction that complex
1118+
numbers do not support order comparison. Within the limits of the types
1119+
involved, they compare mathematically (algorithmically) correct without loss
1120+
of precision.
1121+
1122+
The not-a-number values :const:`float('NaN')` and :const:`Decimal('NaN')`
1123+
are special. They are identical to themselves (``x is x`` is true) but
1124+
are not equal to themselves (``x == x`` is false). Additionally,
1125+
comparing any number to a not-a-number value
10951126
will return ``False``. For example, both ``3 < float('NaN')`` and
10961127
``float('NaN') < 3`` will return ``False``.
10971128

1098-
* Bytes objects are compared lexicographically using the numeric values of their
1099-
elements.
1129+
* Binary sequences (instances of :class:`bytes` or :class:`bytearray`) can be
1130+
compared within and across their types. They compare lexicographically using
1131+
the numeric values of their elements.
1132+
1133+
* Strings (instances of :class:`str`) compare lexicographically using the
1134+
numerical Unicode code points (the result of the built-in function
1135+
:func:`ord`) of their characters. [#]_
1136+
1137+
Strings and binary sequences cannot be directly compared.
1138+
1139+
* Sequences (instances of :class:`tuple`, :class:`list`, or :class:`range`) can
1140+
be compared only within each of their types, with the restriction that ranges
1141+
do not support order comparison. Equality comparison across these types
1142+
results in unequality, and ordering comparison across these types raises
1143+
:exc:`TypeError`.
1144+
1145+
Sequences compare lexicographically using comparison of corresponding
1146+
elements, whereby reflexivity of the elements is enforced.
1147+
1148+
In enforcing reflexivity of elements, the comparison of collections assumes
1149+
that for a collection element ``x``, ``x == x`` is always true. Based on
1150+
that assumption, element identity is compared first, and element comparison
1151+
is performed only for distinct elements. This approach yields the same
1152+
result as a strict element comparison would, if the compared elements are
1153+
reflexive. For non-reflexive elements, the result is different than for
1154+
strict element comparison, and may be surprising: The non-reflexive
1155+
not-a-number values for example result in the following comparison behavior
1156+
when used in a list::
1157+
1158+
>>> nan = float('NaN')
1159+
>>> nan is nan
1160+
True
1161+
>>> nan == nan
1162+
False <-- the defined non-reflexive behavior of NaN
1163+
>>> [nan] == [nan]
1164+
True <-- list enforces reflexivity and tests identity first
1165+
1166+
Lexicographical comparison between built-in collections works as follows:
1167+
1168+
- For two collections to compare equal, they must be of the same type, have
1169+
the same length, and each pair of corresponding elements must compare
1170+
equal (for example, ``[1,2] == (1,2)`` is false because the type is not the
1171+
same).
1172+
1173+
- Collections that support order comparison are ordered the same as their
1174+
first unequal elements (for example, ``[1,2,x] <= [1,2,y]`` has the same
1175+
value as ``x <= y``). If a corresponding element does not exist, the
1176+
shorter collection is ordered first (for example, ``[1,2] < [1,2,3]`` is
1177+
true).
1178+
1179+
* Mappings (instances of :class:`dict`) compare equal if and only if they have
1180+
equal `(key, value)` pairs. Equality comparison of the keys and elements
1181+
enforces reflexivity.
1182+
1183+
Order comparisons (``<``, ``>``, ``<=``, and ``>=``) raise :exc:`TypeError`.
1184+
1185+
* Sets (instances of :class:`set` or :class:`frozenset`) can be compared within
1186+
and across their types.
1187+
1188+
They define order
1189+
comparison operators to mean subset and superset tests. Those relations do
1190+
not define total orderings (for example, the two sets ``{1,2}`` and ``{2,3}``
1191+
are not equal, nor subsets of one another, nor supersets of one
1192+
another). Accordingly, sets are not appropriate arguments for functions
1193+
which depend on total ordering (for example, :func:`min`, :func:`max`, and
1194+
:func:`sorted` produce undefined results given a list of sets as inputs).
11001195

1101-
* Strings are compared lexicographically using the numeric equivalents (the
1102-
result of the built-in function :func:`ord`) of their characters. [#]_ String
1103-
and bytes object can't be compared!
1196+
Comparison of sets enforces reflexivity of its elements.
11041197

1105-
* Tuples and lists are compared lexicographically using comparison of
1106-
corresponding elements. This means that to compare equal, each element must
1107-
compare equal and the two sequences must be of the same type and have the same
1108-
length.
1198+
* Most other built-in types have no comparison methods implemented, so they
1199+
inherit the default comparison behavior.
11091200

1110-
If not equal, the sequences are ordered the same as their first differing
1111-
elements. For example, ``[1,2,x] <= [1,2,y]`` has the same value as
1112-
``x <= y``. If the corresponding element does not exist, the shorter
1113-
sequence is ordered first (for example, ``[1,2] < [1,2,3]``).
1201+
User-defined classes that customize their comparison behavior should follow
1202+
some consistency rules, if possible:
11141203

1115-
* Mappings (dictionaries) compare equal if and only if they have the same
1116-
``(key, value)`` pairs. Order comparisons ``('<', '<=', '>=', '>')``
1117-
raise :exc:`TypeError`.
1204+
* Equality comparison should be reflexive.
1205+
In other words, identical objects should compare equal:
11181206

1119-
* Sets and frozensets define comparison operators to mean subset and superset
1120-
tests. Those relations do not define total orderings (the two sets ``{1,2}``
1121-
and ``{2,3}`` are not equal, nor subsets of one another, nor supersets of one
1122-
another). Accordingly, sets are not appropriate arguments for functions
1123-
which depend on total ordering. For example, :func:`min`, :func:`max`, and
1124-
:func:`sorted` produce undefined results given a list of sets as inputs.
1207+
``x is y`` implies ``x == y``
1208+
1209+
* Comparison should be symmetric.
1210+
In other words, the following expressions should have the same result:
1211+
1212+
``x == y`` and ``y == x``
1213+
1214+
``x != y`` and ``y != x``
1215+
1216+
``x < y`` and ``y > x``
1217+
1218+
``x <= y`` and ``y >= x``
1219+
1220+
* Comparison should be transitive.
1221+
The following (non-exhaustive) examples illustrate that:
1222+
1223+
``x > y and y > z`` implies ``x > z``
11251224

1126-
* Most other objects of built-in types compare unequal unless they are the same
1127-
object; the choice whether one object is considered smaller or larger than
1128-
another one is made arbitrarily but consistently within one execution of a
1129-
program.
1225+
``x < y and y <= z`` implies ``x < z``
11301226

1131-
Comparison of objects of differing types depends on whether either of the
1132-
types provide explicit support for the comparison. Most numeric types can be
1133-
compared with one another. When cross-type comparison is not supported, the
1134-
comparison method returns ``NotImplemented``.
1227+
* Inverse comparison should result in the boolean negation.
1228+
In other words, the following expressions should have the same result:
11351229

1230+
``x == y`` and ``not x != y``
1231+
1232+
``x < y`` and ``not x >= y`` (for total ordering)
1233+
1234+
``x > y`` and ``not x <= y`` (for total ordering)
1235+
1236+
The last two expressions apply to totally ordered collections (e.g. to
1237+
sequences, but not to sets or mappings). See also the
1238+
:func:`~functools.total_ordering` decorator.
1239+
1240+
Python does not enforce these consistency rules. In fact, the not-a-number
1241+
values are an example for not following these rules.
1242+
1243+
1244+
.. _in:
1245+
.. _not in:
11361246
.. _membership-test-details:
11371247

1248+
Membership test operations
1249+
--------------------------
1250+
11381251
The operators :keyword:`in` and :keyword:`not in` test for membership. ``x in
11391252
s`` evaluates to true if *x* is a member of *s*, and false otherwise. ``x not
11401253
in s`` returns the negation of ``x in s``. All built-in sequences and set types
@@ -1176,6 +1289,13 @@ The operator :keyword:`not in` is defined to have the inverse true value of
11761289
operator: is not
11771290
pair: identity; test
11781291

1292+
1293+
.. _is:
1294+
.. _is not:
1295+
1296+
Identity comparisons
1297+
--------------------
1298+
11791299
The operators :keyword:`is` and :keyword:`is not` test for object identity: ``x
11801300
is y`` is true if and only if *x* and *y* are the same object. ``x is not y``
11811301
yields the inverse truth value. [#]_
@@ -1405,12 +1525,24 @@ precedence and have a left-to-right chaining feature as described in the
14051525
cases, Python returns the latter result, in order to preserve that
14061526
``divmod(x,y)[0] * y + x % y`` be very close to ``x``.
14071527
1408-
.. [#] While comparisons between strings make sense at the byte level, they may
1409-
be counter-intuitive to users. For example, the strings ``"\u00C7"`` and
1410-
``"\u0043\u0327"`` compare differently, even though they both represent the
1411-
same unicode character (LATIN CAPITAL LETTER C WITH CEDILLA). To compare
1412-
strings in a human recognizable way, compare using
1413-
:func:`unicodedata.normalize`.
1528+
.. [#] The Unicode standard distinguishes between :dfn:`code points`
1529+
(e.g. U+0041) and :dfn:`abstract characters` (e.g. "LATIN CAPITAL LETTER A").
1530+
While most abstract characters in Unicode are only represented using one
1531+
code point, there is a number of abstract characters that can in addition be
1532+
represented using a sequence of more than one code point. For example, the
1533+
abstract character "LATIN CAPITAL LETTER C WITH CEDILLA" can be represented
1534+
as a single :dfn:`precomposed character` at code position U+00C7, or as a
1535+
sequence of a :dfn:`base character` at code position U+0043 (LATIN CAPITAL
1536+
LETTER C), followed by a :dfn:`combining character` at code position U+0327
1537+
(COMBINING CEDILLA).
1538+
1539+
The comparison operators on strings compare at the level of Unicode code
1540+
points. This may be counter-intuitive to humans. For example,
1541+
``"\u00C7" == "\u0043\u0327"`` is ``False``, even though both strings
1542+
represent the same abstract character "LATIN CAPITAL LETTER C WITH CEDILLA".
1543+
1544+
To compare strings at the level of abstract characters (that is, in a way
1545+
intuitive to humans), use :func:`unicodedata.normalize`.
14141546
14151547
.. [#] Due to automatic garbage-collection, free lists, and the dynamic nature of
14161548
descriptors, you may notice seemingly unusual behaviour in certain uses of

Misc/NEWS

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,13 @@ IDLE
125125
Documentation
126126
-------------
127127

128+
- Issue #12067: Rewrite Comparisons section in the Expressions chapter of the
129+
language reference. Some of the details of comparing mixed types were
130+
incorrect or ambiguous. NotImplemented is only relevant at a lower level
131+
than the Expressions chapter. Added details of comparing range() objects,
132+
and default behaviour and consistency suggestions for user-defined classes.
133+
Patch from Andy Maier.
134+
128135
- Issue #24952: Clarify the default size argument of stack_size() in
129136
the "threading" and "_thread" modules. Patch from Mattip.
130137

0 commit comments

Comments
 (0)