[Python-ideas] Match statement brainstorm
Greg Ewing
greg.ewing at canterbury.ac.nz
Wed May 25 02:26:01 EDT 2016
Nick Coghlan wrote:
> case (.x, .y) as p, q: # Attribute matching
I don't think I like the "as". Like the "?=",
it separates the pattern from the names being bound
too much.
> case (["x"], ["y"]) as p, q: # Item matching
That's even more confusing. The part before the "as"
looks like it should match a tuple of two one-element
lists containing the values "x" and "y".
My feeling is that the patterns should look like
constructors. The archetypal constructor for a mapping
object is the dict display, so a pattern that matches
a mapping having particular keys would be
case {"x": p, "y": q}:
...
> case MATCH_PATTERN [as TARGET] [and CONDITION]:
That way of handling guards wouldn't allow for multiple
guards on the same case. I would suggest
case PATTERN:
when CONDITION:
...
when CONDITION:
...
Note that this would be different from
case PATTERN:
if CONDITION:
...
elif CONDITION:
...
because failure of all the "when" clauses should cause
the next case to be tried.
--
Greg
More information about the Python-ideas
mailing list