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...

On Wed, Jun 02, 2021 at 01:18:30PM +0200, Alexis Masson wrote:
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

On Wed, Jun 02, 2021 at 01:18:30PM +0200, Alexis Masson wrote:
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