On 2020-10-26 04:56, Dennis Sweeney wrote:
What if the mapping assignment were more harmonious with the pattern matching 
PEP? Something like this:

     items = {'eggs': 2, 'cheese': 3, 'spam': 1}
     {'eggs': eggs, 'spam': i_dont_need_to_name_this_spam, **rest} = items
     assert i_dont_need_to_name_this_spam == 1
     assert eggs == 2 and cheese == 3
     assert rest == {'cheese': 3}

The keys here could be arbitrary hashables and the "values" could be arbitrary assignment 
targets (assigned all-or-nothing). This wouldn't need the right-hand-side double-star, and I think 
it more closely resembles the sequence unpacking assignment syntax. You can assign to a (thing that 
looks like a) tuple or to a (thing that looks like a) list or to a sequence subscript or object 
attribute, why not be able to assign to a (thing that looks like a) dictionary? This also avoids 
polluting the local namespace in case one of your keys is the string "range" or 
something. It also feels less magical to me, albeit more verbose.

Calls to a hypothetical parse/sscanf function could closely mirror some 
str.format() calls:

     text = "{a}, {b}, {c}".format(**{'a': a0, 'b': b0, 'c': c0})
     {'a': a1, 'b': b1, 'c': c1} = "{a}, {b}, {c}".parse(text)
     assert (a1, b1, c1) == (a0, b0, c0)

Alternative positional parsing would be useful as well, as in:

     text = "{}, {}, {}".format(a0, b0, c0)
     a1, b1, c1 = "{}, {}, {}".parse(text)
     assert (a1, b1, c1) == (a0, b0, c0)

The assertions could still fail because there's nothing there to say that a0, b0 and c0 are strings, or, indeed, that there isn't a comma in one of them.

[snip]
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/KQLGNEHBEOWRNXG365E2BL5LZQSK45ER/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to