summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRaymond Hettinger <rhettinger@users.noreply.github.com>2019-08-24 17:43:55 (GMT)
committerGitHub <noreply@github.com>2019-08-24 17:43:55 (GMT)
commitedd21129dd304e178ca8be82ba689488dfb58276 (patch)
tree9b513d455578e5cf6cfa31d3604aae401796ab6c
parent5e63ab05f114987478a21612d918a1c0276fe9d2 (diff)
downloadcpython-edd21129dd304e178ca8be82ba689488dfb58276.zip
cpython-edd21129dd304e178ca8be82ba689488dfb58276.tar.gz
cpython-edd21129dd304e178ca8be82ba689488dfb58276.tar.bz2
bpo-32118: Simplify docs for sequence comparison (GH-15450)
-rw-r--r--Doc/reference/expressions.rst26
1 files changed, 7 insertions, 19 deletions
diff --git a/Doc/reference/expressions.rst b/Doc/reference/expressions.rst
index 432327a..ee13c5f 100644
--- a/Doc/reference/expressions.rst
+++ b/Doc/reference/expressions.rst
@@ -1425,6 +1425,10 @@ built-in types.
themselves. For example, if ``x = float('NaN')``, ``3 < x``, ``x < 3``, ``x
== x``, ``x != x`` are all false. This behavior is compliant with IEEE 754.
+* ``None`` and ``NotImplemented`` are singletons. :PEP:`8` advises that
+ comparisons for singletons should always be done with ``is`` or ``is not``,
+ never the equality operators.
+
* Binary sequences (instances of :class:`bytes` or :class:`bytearray`) can be
compared within and across their types. They compare lexicographically using
the numeric values of their elements.
@@ -1442,25 +1446,9 @@ built-in types.
:exc:`TypeError`.
Sequences compare lexicographically using comparison of corresponding
- elements, whereby reflexivity of the elements is enforced.
-
- In enforcing reflexivity of elements, the comparison of collections assumes
- that for a collection element ``x``, ``x == x`` is always true. Based on
- that assumption, element identity is compared first, and element comparison
- is performed only for distinct elements. This approach yields the same
- result as a strict element comparison would, if the compared elements are
- reflexive. For non-reflexive elements, the result is different than for
- strict element comparison, and may be surprising: The non-reflexive
- not-a-number values for example result in the following comparison behavior
- when used in a list::
-
- >>> nan = float('NaN')
- >>> nan is nan
- True
- >>> nan == nan
- False <-- the defined non-reflexive behavior of NaN
- >>> [nan] == [nan]
- True <-- list enforces reflexivity and tests identity first
+ elements. The built-in containers typically assume identical objects are
+ equal to themselves. That lets them bypass equality tests for identical
+ objects to improve performance and to maintain their internal invariants.
Lexicographical comparison between built-in collections works as follows: