[Python-ideas] adding dictionaries

Nick Coghlan ncoghlan at gmail.com
Tue Jul 29 09:46:56 CEST 2014


On 29 July 2014 13:13, Stephen J. Turnbull <stephen at xemacs.org> wrote:
> Alexander Heger writes:
>
>  > I mistyped.  It should have read " ... the behaviour in place for
>  > collections.Counter"
>
> But there *is* a *the* (ie, unique) "additive" behavior for Counter.
> (At least, I find it reasonable to think so.)  What you're missing is
> that there is no such agreement on what it means to add dictionaries.
>
> True, you can "just pick one".  Python doesn't much like to do that,
> though.  The problem is that on discovering that dictionaries can be
> added, *everybody* is going to think that their personal application
> is the obvious one to implement as "+" and/or "+=".  Some of them are
> going to be wrong and write buggy code as a consequence.

In fact, the existence of collections.Counter.__add__ is an argument
*against* introducing dict.__add__ with different semantics:

    >>> issubclass(collections.Counter, dict)
    True

So, if someone *wants* a dict with "addable" semantics, they can
already use collections.Counter. While some of its methods really only
work with integers, the addition part is actually usable with
arbitrary addable types.

If set-like semantics were added to dict, it would conflict with the
existing element-wise semantics of Counter.

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia


More information about the Python-ideas mailing list