On 08/07/2020 19:08, Antoine Pitrou wrote:
On Wed, 8 Jul 2020 18:38:12 +0100
Rhodri James <rho...@kynesim.co.uk> wrote:
On 08/07/2020 16:02, Guido van Rossum wrote:
Today I’m happy (and a little trepidatious) to announce the next
version of PEP 622, Pattern Matching.
Thank you very much to everyone who has been working on this, it is much
appreciated. I have one suggestion for the text: could the section on
Capture Patterns emphasise that only simple (i.e not dotted) names are
capture patterns? The simplified grammar is (fairly) clear and the
later section on Constant Value Patterns should make it obvious, but
somehow when reading version 1 I still managed to miss it. I was quite
surprised when it was pointed out that
case (point.x, point.y):
wasn't going to do what I expected!
Why did you expect? It's not clear to me what it should do at all :-)
I was expecting it to unpack a 2-tuple(/sequence?) into the x and y
attributes of this point object I happened to have in my back pocket
(assuming it matched, of course). It actually matches a 2-tuple against
the values of those attributes (i.e. they are constant value patterns).
It's obvious enough once you have time to read the PEP properly -- I
blame only having so many minutes of reading time in my
compile/download/test cycle!
To use code(ish) examples rather than confusable words, suppose we have:
class Point:
def __init__(self):
self.x = 0
self.y = 0
point = Point()
INCOMING = (1, 2)
match INCOMING:
case (point.x, point.y):
print("Point @", point.x, point.y)
case _:
print("Default")
I expected printout of:
Point @ 1 2
but I would actually get
Default
If on the other hand INCOMING was (0, 0), I would get
Point @ 0 0
because the first case is in fact the equivalent of
case (0, 0):
Obviously this is a pointless example (pun intended) because you would
use a class pattern if you really wanted to do something like what I
first thought of.
--
Rhodri James *-* Kynesim Ltd
_______________________________________________
Python-Dev mailing list -- python-dev@python.org
To unsubscribe send an email to python-dev-le...@python.org
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at
https://mail.python.org/archives/list/python-dev@python.org/message/5IWR4AM7YFXG2CK4IHTAJB45FTBXH3AK/
Code of Conduct: http://python.org/psf/codeofconduct/