[Python-ideas] Unpacking a dict
Davin Potts
python+python_ideas at discontinuity.net
Thu May 26 11:25:10 EDT 2016
> So `a, b, c = **mapping` would be perfectly in line with this.
Your `func` example is a great connection to have made but I would not
reach the same conclusion. When calling `func(**some_dict)` we are not
performing variable assignment but with the `a, b, c = **mapping` we would
be. Whereas I accept that `func(**some_dict)` imposes some constraints on
the nature of the keys in the dict because functions must have proper
variable names as input parameters, I find it difficult to accept a similar
constraint on general dict unpacking.
Davin
On Thu, May 26, 2016 at 7:27 AM, Koos Zevenhoven <k7hoven at gmail.com> wrote:
> 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>
> wrote:
> >> 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
> s
>
> func(x=1, y=2, z=3)
>
> and
>
> 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
>
> Or
>
> 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/
>
>
> _______________________________________________
> 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/d892b54f/attachment.html>
More information about the Python-ideas
mailing list