[Python-ideas] adding dictionaries

Nick Coghlan ncoghlan at gmail.com
Tue Jul 29 00:40:02 CEST 2014


On 29 Jul 2014 08:22, "Alexander Heger" <python at 2sn.net> wrote:

>
> My point is to make the dict() data structure more easy to use for
> most users and use cases.  Especially novices.
> This is what adds power to the language.  Not that you can do things
> (Turing machines can) but that you can do them easily and naturally.

But why is dict merging into a *new* dict something that needs to be done
as a single expression? What's the problem with spelling out "to merge two
dicts into a new, first make a dict, then merge in the other one":

    x = dict(a)
    x.update(b)

That's the real competitor here, not the more cryptic "x = dict(a, **b)"

You can even use it as an example of factoring out a helper function:

    def copy_and_update(a, *args):
        x = dict(a)
        for arg in args:
           x.update(arg)
        return x

My personal experience suggests that's a rare enough use case that it's
fine to leave it as a trivial helper function that people can write if they
need it. The teaching example isn't compelling, since in the teaching case,
spelling out the steps is going to be necessary anyway to explain what the
function or method call is actually doing.

Cheers,
Nick.

> _______________________________________________
> 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/446a86ff/attachment.html>


More information about the Python-ideas mailing list