[Python-ideas] Match statement brainstorm
Greg Ewing
greg.ewing at canterbury.ac.nz
Thu May 26 19:10:08 EDT 2016
Nikolaus Rath wrote:
> I think its much
> better to embed the target variables in the pattern - we just need a way
> to mark them as such.
>
> maybe we could use $?
I'd like to suggest using '?' instead. It looks more pattern-
matchy to me.
Trying to come up with some real-looking examples:
switch obj:
case Point(?x, ?y):
print("Point at", x, y)
case Line(start = ?p1, end = ?p2, width = ?w):
print("Line from", p1, "to", p2, "with width", w)
case Intersection(?obj1, ?obj2):
print("Intersection of", obj1, "and", obj2)
switch command:
case ("frobulate", ?inp, ?out):
print("Frobulating", inp", "to give", out)
case ("frobulate", "-v", ?inp, ?out):
print("Frobulating", inp, "verbosely to give", out)
case ("xyzzy", ?)
print("Xyzzying not implemented, sorry.")
else:
print("Couldn't grok your command, master.")
Here I've also used '?' without a following name as a
"don't care" marker.
--
Greg
More information about the Python-ideas
mailing list