[Python-ideas] Unpacking a dict

Koos Zevenhoven k7hoven at gmail.com
Thu May 26 07:45:07 EDT 2016


On Thu, May 26, 2016 at 1:52 AM, Michael Selik <michael.selik at gmail.com> wrote:
[...]
> On Wed, May 25, 2016 at 4:14 PM Ethan Furman <ethan at stoneleaf.us> wrote:
>>
>> The proposal is this:
>>    a, b = **mapping
>>
>> The advantages:
>> - much more readable
>> - less duplication
>
>
> Why doesn't that work for tuple unpacking?
>     py> a, b = *iterable
>     SyntaxError
>
> Whatever the reasons, that syntax wasn't chosen for tuple unpacking. Dict
> unpacking should mimic tuple unpacking. If I saw ``a, b = **mapping`` I
> would expect ``a, b = *iterable``.
>

I think it would make sense to allow

a, b = *iterable

That would be more explicit about unpacking the iterable. Still, in
clear cases like `a, b = 1, 2`, one could omit the asterisk.

After all, this already works:

a, b, c = 1, *(2, 3)

Some more examples:

a, b = 1, 2    # this is clear
a, b = *(1,2) # could be legal and equivalent to the above
a = *(1, 2)   # would fail, and should fail!
a = 1, 2       # does not fail

So why not allow being more explicit about unpacking?

-- Koos


More information about the Python-ideas mailing list