[Python-ideas] Allow using ** twice
Haoyi Li
haoyi.sg at gmail.com
Thu Jun 6 17:13:59 CEST 2013
-1 for multiple **, +1 for easy merging of dicts, since that would let you
trivially do **(dict_a + dict_b) or similar. Looking at
http://stackoverflow.com/questions/38987/how-can-i-merge-union-two-python-dictionaries-in-a-single-expression
You have a bunch of different solutions that all work with varying degrees
of obtuseness. The least obtuse being:
dict(x,**y)
But it's still entirely non-obvious what the intent of this statement is
("combine these dicts").
I agree that the multiple-key issue is a problem, but I think that
providing support for the "common" case (whichever that may be, i suspect
it's "dicts on right take priority over dicts on left") will be beneficial.
Now merging two dicts takes far too much code and boilerplate, and there
are far too many equivalent ways:
dict(x.items() + y.items())
{xi:(x[xi] if xi not in list(y.keys())
else y[xi]) for xi in list(x.keys())+(list(y.keys()))}
def dict_merge(a, b):
c = a.copy()
c.update(b)
return c
new = dict_merge(old, extras)
dict(list(dict1.items()) + list(dict2.items()))
from collections import ChainMap; ChainMap(y, x)
of performing an operation that is almost (not quite) as fundamental as
+ing lists and &ing sets.
On Thu, Jun 6, 2013 at 10:54 AM, Markus Unterwaditzer <
markus at unterwaditzer.net> wrote:
> This indicates for me that it generally should be possible to generate the
> union of two dicts with sth like {} + {}.
>
> -- Markus (from phone)
>
> Ram Rachum <ram.rachum at gmail.com> wrote:
> >I'd like to be able to use ** twice (using 2 different dicts) when
> >calling
> >a function, and have them both feed arguments into the function.
> >
> >
> >------------------------------------------------------------------------
> >
> >_______________________________________________
> >Python-ideas mailing list
> >Python-ideas at python.org
> >http://mail.python.org/mailman/listinfo/python-ideas
>
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> http://mail.python.org/mailman/listinfo/python-ideas
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20130606/9b09495e/attachment-0001.html>
More information about the Python-ideas
mailing list