Pierre Quentel <[email protected]> added the comment:
Thanks for the explanations, but I feel unconfortable with the fact that
variable-length sequence patterns are implemented the same as unpacking. (sorry
if this has been discussed before, I can't find references to the discussions
that lead to the current version of the PEP).
There are obvious similarities between
[x, *y, z] = A
and
match A:
case [x, *y, z]:
print('ok')
but a big difference, put forward in PEP 634 : the classes supported for
pattern matching are limited (unpacking a generator expression is possible, but
they are not supported as subjects for sequence patterns), and the PEP
explicitely refers to them having a len().
It seems to me that this implies that the algorithms should be different:
- for unpacking, the iterable must be completely consumed before binding the
names on the left-hand side. If it is infinite, unpacking fails
- for variable-length sequence pattern matching (this is how I understand the
last paragraph about them in PEP 634):
. get the subject length
. iterate one by one before the star pattern
. iterate (len(subject) - number of non-star patterns) times for the star
pattern
. iterate one by one after the star pattern
In the second case, even if the subject never raises StopIteration, the match
succeeds.
Does this make sense ?
----------
_______________________________________
Python tracker <[email protected]>
<https://bugs.python.org/issue44741>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe:
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com