On Sat, Nov 14, 2020 at 7:54 AM Nick Coghlan <ncoghlan@gmail.com> wrote:
On Sat, 14 Nov 2020 at 09:51, Greg Ewing <greg.ewing@canterbury.ac.nz> wrote:
On 14/11/20 7:45 am, Brandt Bucher wrote:
with (using your own syntactic flavor): ``` case >first, *>middle, >last: rebuilt = first, *middle, last case {"key": >value, **>rest}: rebuilt = {"key": value, **rest} case Point(x=>a, y=>b): rebuilt = Point(x=a, y=b)
I think this is a case where syntax matters. To my eyes this looks far less confusing:
case ?first, *?middle, ?last: rebuilt = first, *middle, last case {"key": ?value, **?rest}: rebuilt = {"key": value, **rest} case Point(x=?a, y=?b): rebuilt = Point(x=a, y=b)
Based on the discussion in this thread, v3 of PEP 642 is going to propose spelling these as:
case first, *middle, last: rebuilt = first, *middle, last case {"key" as value, **rest}: rebuilt = {"key": value, **rest} case Point(x as a, y as b): rebuilt = Point(x=a, y=b)
I'm in agreement with the PEP 634 authors that we want to keep sequence pattern matching consistent with iterable unpacking as far as name binding is concerned. Mapping patterns and class patterns are both completely new though, and I think the readability problem there is quite similar to the one that existed with walrus patterns in PEP 622, and thus amenable to a similar solution (i.e. spell the capture patterns for those cases with `as`, not `:` or `=`).
FWIW, I'd like to add my +1 to usage of "as" for spelling class capture patterns. This is by far the clearest and easiest to read form I've seen thus far, and I suspect that it would be the easiest to explain to users already familiar with usage of "as" from other areas in Python. A new feature being as distinguishable as possible and easy to explain to existing users is very important in my book, and based on the responses, I think that the current "=" form used in PEP 634 for matching class patterns would be substantially more difficult for users to mentally parse and understand compared to "as". It's also worth considering new Python users that might have general OO experience but not with robust pattern matching (e.g. Java-heavy backgrounds). I could definitely see "case Point(x=a, y=b):" being confused for instantiation, whereas usage of "as" makes it more clear that something else is happening (hopefully leading them to search around for more info about Python pattern matching).