On Sun, Jul 12, 2020 at 11:04 AM Guido van Rossum <[email protected]> wrote:
>
> On Sat, Jul 11, 2020 at 5:58 PM Chris Angelico <[email protected]> wrote:
>>
>> On Sun, Jul 12, 2020 at 10:30 AM Greg Ewing <[email protected]> 
>> wrote:
>> >
>> > Just had another thought about marking assignment targets.
>> >
>> > The PEP currently forbids repeating bound names in a pattern
>> > to avoid raising expectations that
>> >
>> >      case Point(x, x):
>> >
>> > would match only if the two arguments were equal.
>> >
>> > But if assignment targets were marked, we could write this as
>> >
>> >      case Point(?x, x):
>> >
>> > and it would work as expected.
>>
>> Hang on. Matching happens before assignment, so this should use the
>> previous value of x for the matching. At least, that's my
>> understanding. If you do something like:
>>
>> case Point(x, 2):
>>
>> it won't assign x unless the second coordinate is 2, right?
>
>
> Good catch. That's actually undefined -- we want to let the optimizer have 
> some leeway in how to generate  the best code for matching. See 
> https://www.python.org/dev/peps/pep-0622/#performance-considerations
>
> Currently it doesn't optimize all that much -- it just processes patterns 
> from left to right:
> ```
> >>> match Point(3, 3):
> ...   case Point(x, 42): pass
> ...
> >>> print(x)
> 3
> >>>
> ```
>

Ah, okay. My "obvious" intuition was that this wouldn't assign, and
Greg's equally "obvious" intuition was that it would. I think that
disagreement should be a strike against the "Point(?x, x)" notation -
I can't be the only person who would misinterpret it.

ChrisA
_______________________________________________
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/DT2RBMJ2BO2FG5PBWCNGID2DPYYMVQV5/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to