On 15/07/2020 12:37, Mohammad Foroughi via Python-Dev wrote:
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.
Your mailer mangled your example a bit, but it's fairly clear all the
same. The problem with using a keyword is that it starts getting really
ungainly with complex patterns:
match value:
case global x:
print("value matches")
case Point(global x, y):
print("value matches x, y captured")
case Line(Point(global x1, y1), Point(x2, global y2)):
print("getting a bit hard to read here")
Probably the simplest thing to do is to use a namespace to force value
comparison.
from types import SimpleNamespace
# ... stuff ...
const = SimpleNamespace(x=x)
match value:
case const.x:
print("value matches")
case Point(const.x, y):
my_useful_struct.y = y
It's not ideal, but it's fairly clear what's going on if you choose your
names carefully. (And damn it, I've argued myself round to the position
I was trying to argue Tim out of a few weeks ago. Curses!)
--
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/XVUUS4D5JMG5MWWGWPVDV6XNGR34AU6U/
Code of Conduct: http://python.org/psf/codeofconduct/