Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
edd21129
Commit
edd21129
authored
Aug 24, 2019
by
Raymond Hettinger
Committed by
GitHub
Aug 24, 2019
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
bpo-32118: Simplify docs for sequence comparison (GH-15450)
parent
5e63ab05
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
7 additions
and
19 deletions
+7
-19
Doc/reference/expressions.rst
Doc/reference/expressions.rst
+7
-19
No files found.
Doc/reference/expressions.rst
View file @
edd21129
...
...
@@ -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:
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment