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

2020年6月24日 07:49:33 -0700

On 06/23/2020 09:01 AM, Guido van Rossum wrote:
PEP 622
Okay, I took Paul Moore's advice and went looking in existing code for some 
examples, and came across the following:
 if isinstance(v, int):
 # v is an offset
 elif isinstance(v, str):
 # v is a docstring
 elif isinstance(v, tuple) and len(v) in (2, 3) and isinstance(v[0], 
baseinteger) and isinstance(v[1], (basestring, NoneType)):
 # v is an offset, a docstring, and (maybe) a default
 elif isinstance(v, tuple) and len(v) in (1, 2) and isinstance(v[0], 
(basestring, NoneType)):
 # v is a docstring and (maybe) a default
That seems like it would be a perfect match (hah) for the new syntax, but I am 
not confident in my efforts.
This is what I started with:
 match v: # goal here is to turn v into an (offset, docstring, default 
value)
 case int:
 v = v, None, None
 case str:
 v = None, v, None
 case (str, ):
 # how to combine with above case?
 v = None, v[0], None
 case (int, str):
 v += (None, )
 case (int, str, default):
 pass
 case (str, default):
 v = None, v[0], v[1]
Which got me to here:
 match v: # goal here is to turn v into an (offset, docstring, default 
value)
 case int(offset):
 v = offset, None, None
 case str(doc) | (str(doc), ):
 v = None, doc, None
 case (int(offset), str(doc)):
 v = offset, doc, None
 case (int(offset), str(doc), default):
 # already correct
 pass
 case (str(doc), default):
 v = None, doc, default
Is this correct?
Side note: I would much rather read "case str(doc) or (str(doc), )" instead of 
a |.
--
~Ethan~
_______________________________________________
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/N6XLWOSRAVUF6MOOQFZTVQHXEWFRXTHY/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to