@@ -822,7 +822,7 @@ and binds no name. Syntax:
822
822
823
823
``_ `` is a :ref: `soft keyword <soft-keywords >` within any pattern,
824
824
but only within patterns. It is an identifier, as usual, even within
825
- ``match `` headers , ``guards `` , and ``case `` blocks.
825
+ ``match `` subject expressions , ``guard `` \ s , and ``case `` blocks.
826
826
827
827
In simple terms, ``_ `` will always succeed.
828
828
@@ -900,8 +900,8 @@ sequence pattern.
900
900
The following is the logical flow for matching a sequence pattern against a
901
901
subject value:
902
902
903
- #. If the subject value is not an instance of a
904
- :class: ` collections.abc.Sequence ` the sequence pattern fails.
903
+ #. If the subject value is not a sequence [ # ]_, the sequence pattern
904
+ fails.
905
905
906
906
#. If the subject value is an instance of ``str ``, ``bytes `` or ``bytearray ``
907
907
the sequence pattern fails.
@@ -943,7 +943,7 @@ subject value:
943
943
In simple terms ``[P1, P2, P3, `` ... ``, P<N>] `` matches only if all the following
944
944
happens:
945
945
946
- * `` isinstance( <subject>, collections.abc.Sequence) ``
946
+ * check `` <subject> `` is a sequence
947
947
* ``len(subject) == <N> ``
948
948
* ``P1 `` matches ``<subject>[0] `` (note that this match can also bind names)
949
949
* ``P2 `` matches ``<subject>[1] `` (note that this match can also bind names)
@@ -975,8 +975,7 @@ runtime error and will raise :exc:`ValueError`.)
975
975
The following is the logical flow for matching a mapping pattern against a
976
976
subject value:
977
977
978
- #. If the subject value is not an instance of :class: `collections.abc.Mapping `,
979
- the mapping pattern fails.
978
+ #. If the subject value is not a mapping [# ]_,the mapping pattern fails.
980
979
981
980
#. If every key given in the mapping pattern is present in the subject mapping,
982
981
and the pattern for each key matches the corresponding item of the subject
@@ -993,7 +992,7 @@ subject value:
993
992
In simple terms ``{KEY1: P1, KEY2: P2, ... } `` matches only if all the following
994
993
happens:
995
994
996
- * `` isinstance( <subject>, collections.abc.Mapping) ``
995
+ * check `` <subject> `` is a mapping
997
996
* ``KEY1 in <subject> ``
998
997
* ``P1 `` matches ``<subject>[KEY1] ``
999
998
* ... and so on for the corresponding KEY/pattern pair.
@@ -1526,6 +1525,35 @@ body of a coroutine function.
1526
1525
there is a :keyword: `finally ` clause which happens to raise another
1527
1526
exception. That new exception causes the old one to be lost.
1528
1527
1528
+ .. [# ] In pattern matching, a sequence is defined as one of the following:
1529
+
1530
+ * a class that inherits from :class: `collections.abc.Sequence `
1531
+ * a Python class that has been registered as :class: `collections.abc.Sequence `
1532
+ * a builtin class that has its (CPython) :data: `Py_TPFLAGS_SEQUENCE ` bit set
1533
+ * a class that inherits from any of the above
1534
+
1535
+ The following standard library classes are sequences:
1536
+
1537
+ * :class: `array.array `
1538
+ * :class: `collections.deque `
1539
+ * :class: `list `
1540
+ * :class: `memoryview `
1541
+ * :class: `range `
1542
+ * :class: `tuple `
1543
+
1544
+ .. note :: Subject values of type ``str``, ``bytes``, and ``bytearray``
1545
+ do not match sequence patterns.
1546
+
1547
+ .. [# ] In pattern matching, a mapping is defined as one of the following:
1548
+
1549
+ * a class that inherits from :class: `collections.abc.Mapping `
1550
+ * a Python class that has been registered as :class: `collections.abc.Mapping `
1551
+ * a builtin class that has its (CPython) :data: `Py_TPFLAGS_MAPPING ` bit set
1552
+ * a class that inherits from any of the above
1553
+
1554
+ The standard library classes :class: `dict ` and :class: `types.MappingProxyType `
1555
+ are mappings.
1556
+
1529
1557
.. [# ] A string literal appearing as the first statement in the function body is
1530
1558
transformed into the function's ``__doc__ `` attribute and therefore the
1531
1559
function's :term: `docstring `.
0 commit comments