On Thu, Oct 22, 2020 at 09:11:57PM -0700, Guido van Rossum wrote:
> Oh, that's quite different than mapping patterns in PEP 634. :-(
That wasn't intentional, and to be honest I hadn't noticed the mapping
patterns in 634 as yet. (There's a lot in that PEP.)
Having read that now, I think I see the differences:
(1) PEP 634 pattern matches on both the key and/or the value, where keys
must be literals:
case {"sleep": duration}: # From PEP 636, the tutorial.
matches on a key "sleep" and captures the value in `duration`.
My suggestion uses the key as an assignment target, and captures the
value.
Bringing them into alignment:
'sleep': duration = **items
would look up key "sleep" and bind it to variable "duration".
(2) PEP 634 ignores extra keys by default. My suggestion doesn't.
(3) PEP 634 mandates that key:value pairs are looked up with the `get`
method. I didn't specify the method, but I expected it would probably
involves `keys()` and `__getitem__`.
(4) PEP 634 declares that duplicate keys would raise ValueError (at
runtime?). My suggestion would raise SyntaxError at compile-time.
I gave this suggestion earlier:
pattern = "I'll have {main} and {extra} with {colour} coffee."
string = "I'll have spam and eggs with black coffee."
main, extra, colour = **scanf(pattern, string)
The assumption is that scanf would return a dict:
{'main': 'spam', 'extra': 'eggs', 'colour': 'black'}
Using match...case syntax, we would write:
# correct me if I have this wrong
match scanf(pattern, string):
case {'main': main, 'extra': extra, 'colour': colour}:
...
To bring my suggestion into alignment with PEP 634, I could write:
{'main': main, 'extra': extra, 'colour': colour} = **scanf(pattern, string)
My other example was:
def method(self, **kwargs):
spam, eggs, **kw = **kwargs
process(spam, eggs)
super().method(**kw)
Using match...case syntax, we would write:
def method(self, **kwargs):
match kwargs:
case {'spam': spam, 'eggs': eggs, **kw}:
process(spam, eggs)
super().method(**kw)
Using PEP 634 syntax, I could write:
def method(self, **kwargs):
{'spam': spam, 'eggs': eggs, **kw} = **kwargs
process(spam, eggs)
super().method(**kw)
--
Steve
_______________________________________________
Python-ideas mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at
https://mail.python.org/archives/list/[email protected]/message/PWMQFMZUCOJWHXNY2KTAZLHFF5IDXYWN/
Code of Conduct: http://python.org/psf/codeofconduct/