Re: The Pattern Matching Wildcard Is A Bad Idea
Hey everyone, I meant to say, I agree with the two of you, actually. `_` as a variable name is terrible, but it /can/ be used all the same ; on the other hand, wildcards should not be legitimate variable names, to prevent misunderstandings / accidents / bugs. Had I taken part in the discussion aroud pattern matching, I would have liked the `else` syntax better : having a special syntax for special cases seems clearer, cleaner, and safer. Another option that comes to mind is using `*` as the wildcard. There is already a precedent for using `*` alone, e.g. in `import` statements. In this case, it means "import everything from this module", as opposed to listing the items you need. I can easily see the parallel with `match` statements, thus making it intuitive to learn and use this new syntax. Anyway, yes, `_` as joker has flaws, the biggest one being to be a resolvable variable name ; what is the expected behaviour when running a script that has (even accidentally) a viariable named `_` ? The question is even more pressing in REPL, since there /is/ necessarily such a variable ! I guess it's too late to change anything, though, so we'll have to get used to it...
From https://docs.python.org/3.10/whatsnew/3.10.html <https://docs.python.org/3.10/whatsnew/3.10.html>: def http_error(status): match status: case 400: return "Bad request" case 404: return "Not found" case 418: return "I'm a teapot" case _: return "Something's wrong with the Internet" The wildcard idea looks just wrong and confusing. What if I want to use it as a variable and match against it like _ = 504? This is how it should be done: def http_error(status): _ = 504 match status: case 400: return "Bad request" case 404: return "Not found" case 418: return "I'm a teapot" case _: return "Gateway Timeout" else: return "Something's wrong with the Internet"
Don't do that.
There is a very strong convention in Python that a single underscore is to be used for two purposes (and a third in the interactive interpreter only):
(1) For "don't care" variables; anything that you don't care about and aren't intending to use should be named `_`.
first, *_, last = sequence
This tells me that I am only using the first and last items, everything else is ignored.
(2) For internationalisation. This is a convention that comes from other languages, but it is so widespread that we're kinda stuck with it.
print(_("Try again."))
By convention, the underscore function is used to translate messages into the current location's language.
(3) And in the interactive interpreter, underscore is a special variable which captures the previous result. This doesn't work in scripts.
>>> 5 + 4 9 >>> _*10 90
On Wed, Jun 02, 2021 at 01:18:30PM +0200, Alexis Masson wrote:
Anyway, yes, `_` as joker has flaws, the biggest one being to be a resolvable variable name ; what is the expected behaviour when running a script that has (even accidentally) a viariable named `_` ? The question is even more pressing in REPL, since there /is/ necessarily such a variable !
https://www.python.org/dev/peps/pep-0634/#id3 "A wildcard pattern always succeeds. It binds no name." I'm pretty sure that this means that the intended behaviour is that any existing `_` name will be untouched. # Untested. _ = 'My hovercraft is full of eels.' x = 100 match x: case _: assert _ == 'My hovercraft is full of eels.' I would expect that assertion will pass. Everyone really ought to read the justification for the underscore in PEP 635 before commenting further: https://www.python.org/dev/peps/pep-0635/#id23 -- Steve
participants (2)
-
Alexis Masson -
Steven D'Aprano