[Python-ideas] Adding "+" and "+=" operators to dict
Petr Viktorin
encukou at gmail.com
Thu Feb 12 15:52:07 CET 2015
I don't see ChainMap mentioned in this thread, so I'll fix that:
On Thu, Feb 12, 2015 at 2:32 PM, Juancarlo Añez <apalala at gmail.com> wrote:
> most common cases would be:
>
>
> new_dict = a + b
> old_dict += a
In today's Python, that's:
from collections import ChainMap
new_dict = dict(ChainMap(b, a))
old_dict.update(a)
>> new_dict = {}
>> for old_dict in (a, b, c, d):
>> new_dict.update(old_dict)
>
>
> It would be easier if we could do:
>
> new_dict = {}
> new_dict.update(a, b, c, d)
>
> It would also be useful if dict.update() returned self, so this would be
> valid:
>
> new_dict = {}.update(a, b, c, d)
In today's Python:
new_dict = dict(ChainMap(d, b, c, a))
Many uses don't need the dict() call – e.g. when passing it **kwargs,
or when it's more useful as a view.
Personally, the lack of a special operator for this has never bothered me.
More information about the Python-ideas
mailing list