bpo-32118: Simplify docs for sequence comparison (GH-15450) · python/cpython@edd2112 (original) (raw)

`@@ -1425,6 +1425,10 @@ built-in types.

`

1425

1425

``` themselves. For example, if x = float('NaN'), 3 < x, x < 3, ``x


`1426`

`1426`

```  == x``, ``x != x`` are all false. This behavior is compliant with IEEE 754.

1427

1427

``

``

1428


* ``None`` and ``NotImplemented`` are singletons. :PEP:`8` advises that

``

1429


 comparisons for singletons should always be done with ``is`` or ``is not``,

``

1430

`+

never the equality operators.

`

``

1431

+

1428

1432

`` * Binary sequences (instances of :class:bytes or :class:bytearray) can be

``

1429

1433

` compared within and across their types. They compare lexicographically using

`

1430

1434

` the numeric values of their elements.

`

`@@ -1442,25 +1446,9 @@ built-in types.

`

1442

1446

`` :exc:TypeError.

``

1443

1447

``

1444

1448

` Sequences compare lexicographically using comparison of corresponding

`

1445

``

`-

elements, whereby reflexivity of the elements is enforced.

`

1446

``

-

1447

``

`-

In enforcing reflexivity of elements, the comparison of collections assumes

`

1448

``


 that for a collection element ``x``, ``x == x`` is always true. Based on

1449

``

`-

that assumption, element identity is compared first, and element comparison

`

1450

``

`-

is performed only for distinct elements. This approach yields the same

`

1451

``

`-

result as a strict element comparison would, if the compared elements are

`

1452

``

`-

reflexive. For non-reflexive elements, the result is different than for

`

1453

``

`-

strict element comparison, and may be surprising: The non-reflexive

`

1454

``

`-

not-a-number values for example result in the following comparison behavior

`

1455

``

`-

when used in a list::

`

1456

``

-

1457

``

`-

nan = float('NaN')

`

1458

``

`-

nan is nan

`

1459

``

`-

True

`

1460

``

`-

nan == nan

`

1461

``

`-

False <-- the defined non-reflexive behavior of NaN

`

1462

``

`-

[nan] == [nan]

`

1463

``

`-

True <-- list enforces reflexivity and tests identity first

`

``

1449

`+

elements. The built-in containers typically assume identical objects are

`

``

1450

`+

equal to themselves. That lets them bypass equality tests for identical

`

``

1451

`+

objects to improve performance and to maintain their internal invariants.

`

1464

1452

``

1465

1453

` Lexicographical comparison between built-in collections works as follows:

`

1466

1454

``