[Python-ideas] Match statement brainstorm

Ian Kelly ian.g.kelly at gmail.com
Thu May 19 10:46:33 EDT 2016


On Wed, May 18, 2016 at 10:15 PM, Guido van Rossum <guido at python.org> wrote:
> I guess the tuple structure matching idea is fairly easy to grasp.
>
> The attribute idea would be similar to a duck-type check, though more
> emphasizing data attributes. It would be nice if we could write a
> match that said "if it has attributes x and y, assign those to local
> variables p and q, and ignore other attributes". Strawman syntax could
> be like this:
>
> def demo(arg):
>     switch arg:
>         case (x=p, y=q): print('x=', p, 'y=', q)
>         case (a, b, *_): print('a=', a, 'b=', b)
>         else: print('Too bad')

That the first case would match attributes and not named items seems
surprising to me. That is, just by looking at it I would have expected
it to match {'x': 1, 'y': 2}. This feels more in line with the way
that keyword arguments work, and more consistent with the second case
in that both would be matching contents of collections.


More information about the Python-ideas mailing list