[Python-Dev] collections.Counter __add__ implementation quirk

Vlastimil Brom vlastimil.brom at gmail.com
Mon Nov 23 13:43:15 EST 2015


2015-11-23 7:21 GMT+01:00 Alexander Walters <tritium-list at sdamon.com>:
> collections.Counter.__add__ as a bit of a quirk.
>
> Counters allow for negative numbers.  You can subtract from a counter into
> the negative no problem.  However, if you have a counter with a negative
> value and add it to another counter, and if that value, after addition,
> would still be negative... that value is not included in the resulting
> Counter object.  This is kind of weird, to the point of thinking I had a bug
> in other code for several hours until I went and checked how Counters are
> implemented.
>
> Is there any particular reason counters drop negative values when you add
> them together?  I definitely expected them to act like ints do when you add
> negatives, and had to subclass it to get what I think is the obvious
> behavior.
> _______________________________________________
> Python-Dev mailing list
...
Hi,
this is probably more appropriate for the general python list rathere
then this developers' maillist, however, as I asked a similar question
some time ago, I got some detailed explanations for the the current
design decissions from the original developer; cf.:
https://mail.python.org/pipermail/python-list/2010-March/570618.html

(I didn't check possible changes in Counter since that version (3.1 at
that time).)

hth,
  vbr


More information about the Python-Dev mailing list