This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Implement a `Counter.elements_count` method
Type: enhancement Stage:
Components: Library (Lib) Versions: Python 3.3
process
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: Nosy List: cool-RR, eric.araujo, mrabarnett, rhettinger, vstinner
Priority: low Keywords:

Created on 2011-03-31 21:40 by cool-RR, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Messages (6)
msg132708 - (view) Author: Ram Rachum (cool-RR) * Date: 2011-03-31 21:40
I suggest a `Counter.elements_count()` method should be added, that would give the same answer as `sum(counter.itervalues())`.
msg132710 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2011-03-31 21:47
Thanks for the feature request.  Py3.3 is a long ways away, so I'll leave this open for a while.

Comments in favor:
* Precedent with Smalltalk bags that know their total length

Comments against:
* sum(c.values()) is trivial to write
* fattening the API makes it harder to learn and remember
msg132711 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2011-03-31 21:51
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).

Otherwise, I think that sum(c.values()) is enough and can be documented as an example in the Counter doc.
msg132712 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2011-03-31 21:58
> 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.
msg132719 - (view) Author: Matthew Barnett (mrabarnett) * (Python triager) Date: 2011-03-31 23:50
The name isn't meaningful to me. My preference would be for something like "total_count".
msg133088 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2011-04-05 20:47
After more thought, I've decided to close this feature request.  The operation is utterly trivial and isn't worth fattening the API.  I see no advantage in `c.total_count()` versus `sum(c.values())`.
History
Date User Action Args
2022-04-11 14:57:15adminsetgithub: 55942
2011-04-05 20:47:12rhettingersetstatus: open -> closed
resolution: rejected
messages: + msg133088
2011-04-01 16:46:46eric.araujosetnosy: + eric.araujo
2011-03-31 23:50:38mrabarnettsetnosy: + mrabarnett
messages: + msg132719
2011-03-31 21:58:24rhettingersetmessages: + msg132712
2011-03-31 21:51:53vstinnersetnosy: + vstinner
messages: + msg132711
2011-03-31 21:47:07rhettingersetpriority: normal -> low

messages: + msg132710
2011-03-31 21:40:37cool-RRcreate