On 16/07/2020 08:16, Baptiste Carvello wrote:
Hello,
Le 15/07/2020 à 13:37, Mohammad Foroughi via Python-Dev a écrit :
Hi, I had an idea regarding the pattern matching issue of comparing with
a previous constant variable instead of assigning to a new local
variable. I'm not sure if this has been brought up before but instead of
using a symbol with the case statement what if we used a keyword.
[...]
Other ideas for the keyword are "const" or "static" but these 2 are more
difficult to recognise since they aren't in other parts of python but
the "global" keyword is already implemented so it would be easy to
understand.
What about simply "is", which is already a keyword?
AFAIK "is" has no meaning as a prefix operator as of now, so hopefully
it would not make the grammar ambiguous (how can one check that for sure?).
match color:
case is RED:
print("red")
case is BLUE:
print("blue")
case other:
print(f"unknown color {other}")
or with Rhodri James' example:
match value:
case is x:
print("value matches")
case Point(is x, y):
print("value matches x, y captured")
case Line(Point(is x1, y1), Point(x2, is y2)):
print("wouldn't call that pretty, but not ugly either")
Cheers,
Baptiste
P.S.: granted, it could mislead some users into thinking that the
comparison is done with "is" instead of "=="; but then, patterns are
special in many ways, users will have to just learn them…
Hm, plus ca change... Your last paragraph inevitably suggests actually
using '==' rather than 'is'.
Which is something I suggested way back, but I was firmly asked by Guido
to "drop it". (Which, up to now, I have.)
The specific objection he raised was that e.g. this example from the PEP
case BinaryOp(left=Number(value=x), op=op, right=Number(value=y)):
would become
case BinaryOp(left=Number(value===x), op===op,
right=Number(value===y)):
which is unparseable. Although I can't see why it couldn't be written
case BinaryOp(left=Number(value= ==x), op= ==op,
right=Number(value= ==y)): # multiple spaces here for clarity
Rob Cliffe
_______________________________________________
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/TZEJHFALXL4OUBDYNQ5D6PEALUYHG57I/
Code of Conduct: http://python.org/psf/codeofconduct/
_______________________________________________
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/MYNRDEMBDMUXKY7OPKWMAV4ULHL7U62G/
Code of Conduct: http://python.org/psf/codeofconduct/