negative "counts" in collections.Counter?

Chris Rebert clp2 at rebertia.com
Mon Mar 8 11:16:45 EST 2010


On Mon, Mar 8, 2010 at 4:21 AM, Steven D'Aprano <
steve at remove-this-cybersource.com.au> 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-
>> cybersource.com.au> 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.
<snip>
>> 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:
http://mail.python.org/pipermail/python-list/2009-January/1189466.html

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
multiplicities

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

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

Cheers,
Chris
--
http://blog.rebertia.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20100308/439e862e/attachment-0001.html>


More information about the Python-list mailing list