On Sat, 14 Nov 2020 at 07:55, Joao S. O. Bueno <jsbu...@python.org.br> wrote:
> On Fri, 13 Nov 2020 at 17:36, Jim J. Jewett <jimjjew...@gmail.com> wrote:
>>
>> I *hope* this was a typo!  If
>>
>>     case Point(x=a, y=b):
>>
>> assigns to a and b (instead of x and y, as in a normal call), then that is 
>> ... going to be very easy for me to forget, and to miss even when I'm aware 
>> of it.
>
>
> No typo -  this is _precisely_what the main proposal on PEPs 634, 635 and 636 
> is suggesting, and tha PEP 642 is trying to avoid.

PEP 642 isn't trying to avoid it yet, as it wasn't something that had
previously occurred to me as a possible problem (assigning to a
"function call" is already illegal syntax, so PEP 642 currently
inherits the spelling of class patterns unchanged from PEP 634, and
the same is also mostly true for mapping patterns).

Now that the point has been raised, though, my idea for addressing it
would be somewhat similar to the fix applied for walrus patterns
between PEP 622 and PEP 634:

* To capture a mapping key or instance attribute, don't use ":" or "=", use "as"
* You would only use ":" or "=" if you wanted to specify a subpattern
to match the value against
* Specifying an irrefutable pattern other than the wildcard pattern on
a mapping key or instance attribute would be a syntax error

That approach would give the following:

    case Point(x as a, y as b):
        ... # A Point object with x & y attributes, capturing those to
"a" and "b"
    case object(x=__, y=__):
        ... # Any object with x & y attributes, not capturing anything
    case object(x=a, y=b):
        ... # Syntax Error (matching instance attribute against a
non-wildcard irrefutable pattern)


    case {"x" as a, "y" as b):
        ... # A mapping with "x" & "y" keys, capturing those to "a" and "b"
    case {"x":__, "y":__}:
        ... # A mapping with "x" & "y" keys, not capturing anything
    case {"x":a, "y":b}:
        ... # Syntax Error (matching mapping value against a
non-wildcard irrefutable pattern)

Cheers,
Nick.

-- 
Nick Coghlan   |   ncogh...@gmail.com   |   Brisbane, Australia
_______________________________________________
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/Q2ARJULLJG6HRCDXR4SSA7K6NLTOPUL7/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to