[Python-ideas] adding dictionaries

Nick Coghlan ncoghlan at gmail.com
Tue Jul 29 00:27:06 CEST 2014


On 29 Jul 2014 08:16, "Alexander Heger" <python at 2sn.net> wrote:
>
> > In all honesty, I'd suggest that code which looks bad enough to
> > warrant even considering this feature is probably badly in need of
> > refactoring, at which point the problem will likely go away.
>
> I often want to call functions with added (or removed, replaced)
> keywords from the call.
>
> args0 = dict(...)
> args1 = dict(...)
>
> def f(**kwargs):
>     g(**(arg0 | kwargs | args1))
>
> currently I have to write
>
> args = dict(...)
> def f(**kwargs):
>     temp_args = dict(dic0)
>     temp_args.update(kwargs)
>     temp_args.update(dic1)
>     g(**temp_args)

The first part of this one of the use cases for functools.partial(), so it
isn't a compelling argument for easy dict merging. The above is largely an
awkward way of spelling:

    import functools
    f = functools.partial(g, **...)

The one difference is to also silently *override* some of the explicitly
passed arguments, but that part's downright user hostile and shouldn't be
encouraged.

Regards,
Nick.

>
> It would also make the proposed feature to allow multiple kw args
> expansions in Python 3.5 easy to write by having
>
> f(**a, **b, **c)
> be equivalent to
> f(**(a | b | c))
>
> -Alexander
> _______________________________________________
> 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/20140729/eaa6a397/attachment.html>


More information about the Python-ideas mailing list