negative "counts" in collections.Counter?

Chris Rebert clp2 at
Mon Mar 8 17:16:45 CET 2010

On Mon, Mar 8, 2010 at 4:21 AM, Steven D'Aprano <
steve at> wrote:
> On Sun, 07 Mar 2010 22:31:00 -0800, Raymond Hettinger wrote:
>> On Mar 7, 5:46 pm, Steven D'Aprano <st... at REMOVE-THIS-
>>> wrote:
>>> Given that Counter supports negative counts, it looks to me that the
>>> behaviour of __add__ and __sub__ is fundamentally flawed. You should
>>> raise a bug report (feature enhancement) on the bug tracker.
>> It isn't a bug.  I designed it that way. There were several possible
>> design choices, each benefitting different use cases.
>> Instead the choice was to implement the four methods as multiset
>> operations.  As such, they need to correspond to regular set operations.
> Personally, I think the behaviour of + and - would be far less surprising
> if the class was called Multiset. Intuitively, one would expect counters
> to be limited to ints, and to support negative counts when adding and
> subtracting. In hindsight, do you think that Multiset would have been a
> better name?

Of course I'm not Raymond, but I'd encourage reading the original
announcement thread where that and related issues were discussed:

To summarize the arguments against "Multiset":
* the API is primarily dict-like, not set-like
** related to this, len() and .items() don't behave in what is arguably an
"intuitive" way for multisets
* the API allows for negative counts; sets don't normally allow negative

The same pretty much also goes for why "Bag" wouldn't be an appropriate

Finally, it leaves the names available for when a proper Bag/Multiset is
added. :)

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <>

More information about the Python-list mailing list