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

2020年6月24日 11:16:12 -0700

On Wed, Jun 24, 2020 at 3:02 AM Chris Jerdonek <[email protected]>
wrote:
> I have an exploratory question. In this section:
>
> The alternatives may bind variables, as long as each alternative binds the
>> same set of variables (excluding _). For example:
>> match something:
>> ...
>> case Foo(arg=x) | Bar(arg=x): # Valid, both arms bind 'x'
>> ...
>> ...
>
>
> Tweaking the above example slightly, would there be a way to modify the
> following so that, if the second alternative matched, then 'x' would have
> the value, say, None assigned to it?
>
> match something:
>> ...
>> case Foo(arg=x) | Bar() (syntax assigning, say, None to x?)
>> ...
>> ...
>
>
> That would let Bar be handled by the Foo case even if Bar doesn't take an
> argument. I'm not sure if this would ever be needed, but it's something I
> was wondering. I didn't see this covered but could have missed it.
>
That sounds like you are planning to put an 'if x is not None' check in the
block. In most cases it would probably be cleaner to separate this out into
two cases. (And yes, I can think of counterexamples, but they don't feel
compelling enough to try and invent such syntax.)
-- 
--Guido van Rossum (python.org/~guido)
*Pronouns: he/him **(why is my pronoun here?)*
<http://feministing.com/2015/02/03/how-using-they-as-a-singular-pronoun-can-change-the-world/>
_______________________________________________
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/POSKBGB55WON55MSEQ4FJYM2YSL3YEN3/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to