[Python-Dev] Re: PEP 622: Structural Pattern Matching

2020年6月23日 23:41:31 -0700

On 06/23/2020 10:11 PM, Ethan Furman wrote:
As others have noted, the leading dot to disambiguate between a name assignment 
and a value check is going to be a problem.
I suspect I wasn't as clear as I could have been, so let me try again (along 
with some thinking-out-loud...).
First premise: the visual difference between BLACK and .BLACK is minuscule, 
and will trip people up.
Second premise: there is no practical difference between
 match color:
 case BLACK:
 # do stuff
and
 match color:
 case _:
 BLACK = color
 # do stuff
Conclusion: there is no need for an always true match, since that's basically the same thing as an 
"else" (or "case _", currently).
Since we don't need an always True match, we don't need to allow a single name after 
"case" as an assignment target, which means we don't need to support ".name" as 
a value target -- the plain name will work as a value target.
Am I just stuck on the single-name scenario and missing the bigger picture? 
What happens here:
 aught = 0
 match an_obj:
 case Point(aught, 6):
 # do stuff
for the aught in Point to match the global aught it needs the dot prefix, 
doesn't it. But this is what guard clauses are for, right?
 aught = 0
 match an_obj:
 case Point(x, 6) if x == aught:
 # do stuff
So we still don't need a leading dot.
Going back to the example and taking a different tack [1]:
 match color:
 case BLACK: # BLACK is the global variable
is really the same as
 match color:
 case color == BLACK:
We've been dropping the "color ==" part, but what if we only drop the "color" 
part?
 match color:
 case == BLACK: # comparing against the global variable
and then
 match color:
 case BLACK: # always True match with `color` assigned to `BLACK`
Equal signs are much easier to notice than a single dot.
Okay, I've pretty much done a 180 here -- a bare name should be an assignment target, but instead of 
having a leading dot be the "this is really an existing variable switch", have an operator be 
that switch: ==, <, >=, etc. Of course "==" will be the most common, so it can be 
dropped as well when the meaning doesn't change in its absence (kind of like parentheses usually being 
optional).
--
~Ethan~
[1] https://en.wikipedia.org/wiki/Tack_(sailing)
_______________________________________________
Python-Dev mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at 
https://mail.python.org/archives/list/[email protected]/message/JSAFZLYATXXETBGR6IK7Z3NAZF2ZS7SJ/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to