[Python-Dev] Should collections.Counter check for int?

Hagen Fürstenau hagenf at CoLi.Uni-SB.DE
Wed May 13 18:07:50 CEST 2009


I just noticed that while the docs say that "Counts are allowed to be
any integer value including zero or negative counts",
collections.Counter doesn't perform any check on the types of count
values. Instead, non-numerical values will lead to strange behaviour or
exceptions later on:

>>> c = collections.Counter({'a':'3', 'b':'20', 'c':'100'})
>>> c.most_common(2)
[('a', '3'), ('b', '20')]
>>> c+c
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/local/hagenf/lib/python3.1/collections.py", line 467, in __add__
    if newcount > 0:
TypeError: unorderable types: str() > int()

I'd prefer Counter to refuse non-numerical values right away as the
present behaviour may hide bugs (e.g. a forgotten string->int
conversion). Any opinions? (And what about negative values or floats?)

- Hagen


More information about the Python-Dev mailing list