On 2020-06-23 20:27, Rob Cliffe via Python-Dev wrote:
On 23/06/2020 17:01, Guido van Rossum wrote:
You can combine several literals in a single pattern using `|` ("or"):
```py case 401|403|404: return "Not allowed"
The PEP is great, but this strikes me as horribly confusing, given that 401|403|404 is already legal syntax. IIUC any legal expression can come between `case` and `:`, but expressions that contain `|` at their outermost level are *interpreted differently* than from in other contexts.
The grammar shows that 'case' is followed by a series of alternatives, separated by '|', but the alternatives aren't expressions, basically only literals and instances of a given class. You can't say "case 1 + 2:", for example.
Presumably adding parentheses: case (401|403|404): would make it equivalent to case 407:
I'm not sure whether that's legal, but it's not equivalent.
Is a separator (other than whitespace) actually needed? Can the parser cope with case 401 403 404:
Failing that IMO preferable, albeit not ideal, possibilities would be 1) Use colon as the separator. 2) Use comma as the separator - this is already legal syntax too, but IMO it reads more naturally. (And IIRC there are already contexts where brackets are necessary to indicate a tuple.) Perhaps someone can think of something better.
I also (with others) prefer `else:` or perhaps `case else:` to using the`_` variable. The latter is obscure, and woudn't sit well with code that already uses that variable for its own purposes.
I think that's done for consistency. '_' is a wildcard and you can have: case (_, _): to match any 2-tuple, so: case _: would match any value, and can thus already serve as the default. I wouldn't object to 'else', though.