From 045450aaf47941f3ee7daaa1774947b31885b2aa Mon Sep 17 00:00:00 2001 From: Guido van Rossum Date: Wed, 1 Jul 2020 22:32:42 -0700 Subject: [PATCH] PEP 622: Strengthen the argument for _ as wildcard (#1488) See https://github.com/gvanrossum/patma/issues/93#issuecomment-650723402 Co-authored-by: Brandt Bucher --- pep-0622.rst | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/pep-0622.rst b/pep-0622.rst index a3694355b..2e2a0eb77 100644 --- a/pep-0622.rst +++ b/pep-0622.rst @@ -981,7 +981,6 @@ A small collection of example code is `available on GitHub `_. - .. _rejected ideas: Rejected Ideas @@ -1384,12 +1383,29 @@ 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 +it would require modifying the tokenizer. + +Also, ``_`` 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: ... +Perhaps the most convincing argument is that ``_`` is used as the +wildcard in every other language we've looked at supporting pattern +matching: C#, Elixir, Erlang, F#, Haskell, Mathematica, OCaml, Ruby, +Rust, Scala, and Swift. Now, in general, we should not be concerned +too much with what another language does, since Python is clearly +different from all these languages. However, if there is such an +overwhelming and strong consensus, Python should not go out of its way +to do something completely different -- particularly given that ``_`` +works well in Python and is already in use as a throwaway target. + +Note that ``_`` is not assigned to by patterns -- this avoids +conflicts with the use of ``_`` as a marker for translatable strings +and an alias for ``gettext.gettext``, as recommended by the +``gettext`` module documentation. + Use some other syntax instead of ``|`` for OR patterns ------------------------------------------------------