[Python-ideas] adding dictionaries

Alexander Heger python at 2sn.net
Tue Jul 29 01:45:06 CEST 2014


> 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.

it is more about having easy operations for people who learn Python
for the sake of using it (besides, I teach science students not
computer science students).

The point is that it could be done in one operation.  It seems like
asking people to write

a = 2 + 3

as

a = int(2)
a.add(3)

Turing machine vs modern programming language.

It does already work for Counters.

The discussion seems to go such that because people can't agree
whether the first or second occurrence of keys takes precedence, or
what operator to use (already decided by the design of Counter) it is
not done at all.  To be fair, I am not a core Python programmer and am
asking others to implement this - or maybe even agree it would be
useful -, maybe pushing too much where just an idea should be floated.

-Alexander


More information about the Python-ideas mailing list