[issue11733] Implement a `Counter.elements_count` method

Raymond Hettinger report at bugs.python.org
Thu Mar 31 23:58:25 CEST 2011


Raymond Hettinger <raymond.hettinger at gmail.com> added the comment:

> It would be interesting to have Counter.elements_count() 
> if it has a complexity of O(1): if the collections 
> maintains a total (in a private attribute).

I agree that it would be interesting; however, it would be an implementation disaster.  We would have to override (and slow-down) every mutating method in order to maintain the invariant.  And as a dict subclass, any C module using PyDict_Setitem() would bypass those methods and still break the invariant no matter what we do.

The Counter tool was designed in an open fashion (a simple, fully exposed dictionary with a few handy methods for counting).  In that regard, it is much different and more flexible than a traditional Bag class that maintains its data privately.

----------

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


More information about the Python-bugs-list mailing list