[Python-ideas] Counter comparison

Raymond Hettinger raymond.hettinger at gmail.com
Sat Nov 22 16:47:51 CET 2014


> On Nov 21, 2014, at 3:55 PM, Antony Lee <antony.lee at berkeley.edu> wrote:
> 
> As Counter objects effectively behave like multi-sets, it seems reasonable to overload <, <=, >, >= as for set objects to check whether a Counter is a sub/super-set of another Counter:
> 
> c < d  <===>  all(c[k] < d[k] for k in c)
> 
> Thoughts

This is something that could be done, but I think the demonstrated need is nearly zero.  It doesn't seem to arise in practice.

Conceptually, a counter is just a dictionary that returns zero instead of raising a KeyError.  So, while they can be used as multisets, they have other uses as well (for example, allowing negative counts or fractional counts).

Another thought, is that overloading comparison operators risks isn't always a good thing.  Even for regular sets, people sometimes get surprised that the sort order isn't deterministic (because sets have a partial ordering instead of a total ordering).

The idea of adding counter comparisons was considered from the outset and was intentionally not included.  If compelling use cases were arising in practice, we could add comparison support, but until then it is more prudent to be conservative with the API.
  

Raymond



-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20141122/73c54ad7/attachment.html>


More information about the Python-ideas mailing list