[issue22533] Counter with no keys does not compare equal to Counter with keys which zero value

Raymond Hettinger report at bugs.python.org
Sat Oct 11 09:19:31 CEST 2014


Raymond Hettinger added the comment:

> It is my thought that a Counter with all its keys set to zero 
> is as empty as a Counter with no keys

If the counter were a stand-alone multiset or bag class, this might be a reasonable thing to do.

However, for better or for worse, the design of the counter is primarily "a dict with a __missing__ that returns 0 and adds some handy methods useful in the context of dicts being used for counting".   This gives it flexibility, simplicity, speed, and makes it more or less substitutable for regular dicts in functions that expect regular dicts.  IIRC, this is the basic design that Guido endorsed when the idea of a counter was first proposed.

> I agree it's sort of weird, but I feel like fixing it will 
> just break a ton of existing code.

It is sort of weird, but that was the intended behavior (trying to keep the counter as dict-like as possible), and you're correct that changing it now would risk either breaking or substantially slowing code existing code.

----------
resolution:  -> not a bug
status: open -> closed

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue22533>
_______________________________________


More information about the Python-bugs-list mailing list