[Python-ideas] dictionary unpacking

Georg Brandl g.brandl at gmx.net
Wed Apr 2 14:21:39 CEST 2008


Facundo Batista schrieb:
> 2008/3/31, Mathias Panzenböck <grosser.meister.morti-hi6Y0CQ0nG0 at public.gmane.org>:
> 
>> Maybe dictionary unpacking would be a nice thing?
>>
>>   >>> d = {'foo': 42, 'egg': 23}
>>   >>> {'foo': bar, 'egg': spam} = d
>>   >>> print bar, spam
>>  42 23
> 
> Bad idea.
> 
> It uncovers a lot of details for your brain to take care  when doing
> that, for example:
> 
>>>> d = {'foo': 42, 'egg': 23}
>>>> {'not': bar, 'egg': spam} = d
>>>> print bar
> ???
>>>> {'egg': bar, 'egg': spam} = d
>>>> print bar
> ???
> 
> I would consider something like the following:
> 
>   bar, spam = d.multiple("foo", "egg")
> 
> with the semantics of:
> 
>   bar, spam = [d[k] for k in ("foo", "egg")]

We also have

bar, spam = itemgetter("foo", "egg")(d)

if some functional form is preferred.

Georg

-- 
Thus spake the Lord: Thou shalt indent with four spaces. No more, no less.
Four shall be the number of spaces thou shalt indent, and the number of thy
indenting shall be four. Eight shalt thou not indent, nor either indent thou
two, excepting that thou then proceed to four. Tabs are right out.




More information about the Python-ideas mailing list