I'm in favor of keeping the PEP as it currently is. Mappings are naturally
structural subtypes of one another, therefore mapping patterns should be
consistent with class patterns.
car = Car(...)
match car:
case Vehicle():
pass
case Car(): # will never match
pass
This example is analogous to the first one in the discussion. If Car is a
subclass of Vehicle, then Vehicle() is a more general pattern than Car() and
will always match despite the instance not being exactly of type Vehicle. With
mapping patterns it's exactly the same thing. You need to match the more
specific patterns first. Matching x and y is more general than matching x, y
and z.
One more thing. If we compare the proposed behavior to other languages the most
relevant example would be object destructuring in javascript:
const { 'x': x, 'y': y } = { 'x': 1, 'y': 2, 'z': 3 };
const { x, y } = { x: 1, y: 2, z: 3 }; // more common short form
Object destructuring only matches the specified fields. You can also match the
remaining fields but it's always explicit:
const { x, y, ...rest } = { x: 1, y: 2, z: 3 };
console.log(rest); // { z: 3 }
The pattern is widely adopted and the behavior generally lines up with people's
expectations.
_______________________________________________
Python-ideas mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at
https://mail.python.org/archives/list/[email protected]/message/52L3EQ5FUBXDZRFAU4D4PESMZ6GUGIHC/
Code of Conduct: http://python.org/psf/codeofconduct/