[Python-ideas] Match statement brainstorm

Nikolaus Rath Nikolaus at rath.org
Fri May 27 11:14:25 EDT 2016


On May 27 2016, Greg Ewing <greg.ewing-F+z8Qja7x9Xokq/tPzqvJg at public.gmane.org> wrote:
> 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)


I think that's a little confusing (but I don't have a better idea).
Assuming you have this class:

class Point:
     def __init__(self, y, x):
         self.y = y
         self.x = x

would your first example assign x == obj.x or x = obj.y? In
ML-like languages these constructs typically match against the
"constructor", so you would expect x == obj.y. But I think the mechanics
of Python would force a translation to x == obj.x.


Best,
-Nikolaus
-- 
GPG encrypted emails preferred. Key id: 0xD113FCAC3C4E599F
Fingerprint: ED31 791B 2C5C 1613 AF38 8B8A D113 FCAC 3C4E 599F

             »Time flies like an arrow, fruit flies like a Banana.«


More information about the Python-ideas mailing list