[Python-ideas] Match statement brainstorm

Greg Ewing greg.ewing at canterbury.ac.nz
Fri May 27 21:32:26 EDT 2016


Nikolaus Rath wrote:

> 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?

The intention is that positional args in the pattern
correspond to positional args in the object's constructor.
To make that work, there will have to be some protocol for
reversing the construction of an object.

It's been suggested that pickle's __getinitargs__ could
be re-used for this purpose. Or we could add a new one
called __unpack__ or __uninit__ or something.

If the object doesn't explicitly say how to unpack
itself, iteration could be used as a fallback.

Since your example above doesn't do either of those,
it would raise an exception when used in a match
statement.

-- 
Greg


More information about the Python-ideas mailing list