[Python-ideas] adding dictionaries

Andrew Barnert abarnert at yahoo.com
Mon Jul 28 22:20:20 CEST 2014


On Jul 28, 2014, at 12:21, Paul Moore <p.f.moore at gmail.com> wrote:

> On 28 July 2014 19:58, Nathan Schneider <nathan at cmu.edu> wrote:
>> Here are two threads that had some discussion of this:
>> https://mail.python.org/pipermail/python-ideas/2011-December/013227.html
> 
> This doesn't seem to have a use case, other than "it would be nice".
> 
>> https://mail.python.org/pipermail/python-ideas/2013-June/021140.html.
> 
> This can be handled using ChainMap, if I understand the proposal.

When the underlying dicts and desired combined dict are all going to be used immutably, ChainMap is the perfect answer. (Better than an "updated" function for performance if nothing else.) And usually, when you're looking for a non-mutating combine-dicts operation, that will be what you want.

But usually isn't always. If you want a snapshot of the combination of mutable dicts, ChainMap is wrong. If you want to be able to mutate the result, ChainMap is wrong.

All that being said, I'm not sure these use cases are sufficiently common to warrant adding an operator--especially since there are other just-as-(un)common use cases it wouldn't solve. (For example, what I often want is a mutable "overlay" ChainMap, which doesn't need to copy the entire potentially-gigantic source dicts. I wouldn't expect an operator for that, even though I need it far more often than I need a mutable snapshot copy.)

And of course, as you say, real-life use cases would be a lot more compelling than theoretical/abstract ones.

>> Seems like a useful feature if there could be a clean way to spell it.
> 
> I've yet to see any real-world situation when I've wanted "dictionary
> addition" (with any of the various semantics proposed here) and I've
> never encountered a situation where using d1.update(d2) was
> sufficiently awkward that having an operator seemed reasonable.
> 
> 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.
> 
> Paul
> _______________________________________________
> 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/


More information about the Python-ideas mailing list