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)

This way, pattern.format() and pattern.parse() would be trying to be inverses 
of each other (as much as is reasonable, probably limited to parsing strings, 
floats and ints).

Then maybe people could get used to a format-string-like mini-language for 
parsing, and eventually, the f-string assignment might be better received, and 
we could propose something like

    text = f"{a0}, {b0}, {c0}"
    f"{a1}, {b1}, {c1}" = text
    assert (a1, b1, c1) == (a0, b0, c0)

as well, where we lose some of the flexibility but gain better D.R.Y. and more 
visual locality, useful in the simple cases. I see the potential for a strong 
analogy:

    positional format() : keyword-based format() : fstrings
    ::
    positional parse() : keyword-based parse(): assignment to fstrings
_______________________________________________
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/H5252OAGH2IWEVQ26F2OWNQZCGMVNZWP/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to