[Python-ideas] Unpacking a dict

Koos Zevenhoven k7hoven at gmail.com
Thu May 26 08:27:55 EDT 2016

On Thu, May 26, 2016 at 5:18 AM, Guido van Rossum <guido at python.org> wrote:
> On Wed, May 25, 2016 at 6:56 PM, Steven D'Aprano <steve at pearwood.info>
>> On Wed, May 25, 2016 at 01:11:35PM +0000, Michael Selik wrote:
>> What is your evidence for this claim? So far I've only seen one real-
>> world use-case for this, and that single use-case would be well served
>> by a simpler syntax:
>> a, b, c = **mapping
> I have to warn here. This looks cool but it does something that AFAIK
> no other Python syntax uses -- it takes variable names and does
> something to those variables but *also* uses their actual names as
> string literals. I agree that the use cases for this seem pretty
> sweet, but perhaps we should give it a somewhat different syntax just
> so it's clear that the names on the LHS matter. The precedent that the
> targets must be actual names rather than anything you can assign to is
> also kind of scary.

I understand the concern, and maybe you are right. However
​, this:​

​    ​
def func(y, z, x)
​        ​
print(x, y, z)

​    ​
func(**dict(x=1, y=2, z=3))

prints "1 2 3"
​, a
​nd so doe
    func(x=1, y=2, z=3)​


​    func(z=3, x=1, y=2)

​So `a, b, c = **mapping` would be perfectly in line with this. Of course
there may still be confusion, but that would mean the user would probably
already be confused about whether dicts are ordered or not, so that
confusion would need to be fixed anyway. I think the key is that ** should
_never_ be interpreted as unpack/repack by order. Or in other words, it
always means unpack/repack _by name_.

That said, here's a couple of suggestions:

    **(a, b, c) = **mapping

    **{a, b, c} = **mapping

Although `a, b, c = **mapping` would still be more convenient.

​-- Koos

PS. For even more explicitness:

a from 'a', b from 'b', c from 'c' = **mapping

Which would allow even

b from 1, a from 0, x from 2 = **iterable


a, b, c from 'a', 'b', 'c' in mapping
b, a, c from 1, 0, 2 in mapping

> --
> --Guido van Rossum (python.org/~guido)
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20160526/e1e41631/attachment-0001.html>

More information about the Python-ideas mailing list