PEP 622: Explain why * and ... don't make good wildcards
(And hint that ? is a little better but still worse than _.)
This commit is contained in:
parent
8f43d3ef64
commit
33788eb974
32
pep-0622.rst
32
pep-0622.rst
|
@ -1425,6 +1425,38 @@ legitimate use for ``_`` as an important global variable, esp. in
|
||||||
i18n) and the only reason for this prohibition was to prevent some
|
i18n) and the only reason for this prohibition was to prevent some
|
||||||
user confusion. But it's not the hill to die on.
|
user confusion. But it's not the hill to die on.
|
||||||
|
|
||||||
|
Use some other token as wildcard
|
||||||
|
--------------------------------
|
||||||
|
|
||||||
|
It has been proposed to use ``...`` (i.e., the ellipsis token) or
|
||||||
|
``*`` (star) as a wildcard. However, both these look as if an
|
||||||
|
arbitrary number of items is omitted::
|
||||||
|
|
||||||
|
case [a, ..., z]: ...
|
||||||
|
case [a, *, z]: ...
|
||||||
|
|
||||||
|
Both look like the would match a sequence of at two or more items,
|
||||||
|
capturing the first and last values.
|
||||||
|
|
||||||
|
In addition, if ``*`` were to be used as the wildcard character, we
|
||||||
|
would have to come up with some other way to capture the rest of a
|
||||||
|
sequence, currently spelled like this::
|
||||||
|
|
||||||
|
case [first, second, *rest]: ...
|
||||||
|
|
||||||
|
Using an ellipsis would also be more confusing in documentation and
|
||||||
|
examples, where ``...`` is routinely used to indicate something
|
||||||
|
obvious or irrelevant. (Yes, this would also be an argument against
|
||||||
|
the other uses of ``...`` in Python, but that water is already under
|
||||||
|
the bridge.)
|
||||||
|
|
||||||
|
Another proposal was to use ``?``. This could be acceptable, although
|
||||||
|
it would require modifying the tokenizer. But ``_`` is already used
|
||||||
|
as a throwaway target in other contexts, and this use is pretty
|
||||||
|
similar. This example is from ``difflib.py`` in the stdlib::
|
||||||
|
|
||||||
|
for tag, _, _, j1, j2 in group: ...
|
||||||
|
|
||||||
|
|
||||||
.. _deferred ideas:
|
.. _deferred ideas:
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue