Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #80278

Real-world use of Counter

Date 2014-11-05 08:26 -0800
From Ethan Furman <ethan@stoneleaf.us>
Subject Real-world use of Counter
Newsgroups comp.lang.python
Message-ID <mailman.15504.1415204817.18130.python-list@python.org> (permalink)

Show all headers | View raw


I'm looking for real-world uses of collections.Counter, specifically to see if anyone has been surprised by, or had to 
spend extra-time debugging, issues with the in-place operators.

Background:

Most Python data types will cause a TypeError to be raised if unusable types are passed in:

--> {'a': 0}.update(5)
TypeError: 'int' object is not iterable

--> [1, 2, 3].extend(3.14)
TypeError: 'float' object is not iterable

--> from collections import Counter
--> Counter() + [1, 2, 3]
TypeError: unsupported operand type(s) for +: 'Counter' and 'list'

Most Counter in-place methods also behave this way:

--> c /= [1, 2, 3]
TypeError: unsupported operand type(s) for /=: 'Counter' and 'list'

However, in the case of a handful of Counter in-place methods (+=, -=, &=, and |=), this is what happens instead:

--> c = Counter()
--> c += [1, 2, 3]
AttributeError: 'list' object has no attribute 'items'

Even worse (in my opinion) is the case of an empty Counter `and`ed with an incompatible type:

--> c &= [1, 2, 3]
-->

No error is raised at all.

In order to avoid unnecessary code churn (the fix itself is quite simple), the maintainer of the collections module 
wants to know if anybody has actually been affected by these inconsistencies, and if so, whether it was a minor 
inconvenience, or a compelling use-case.

So, if this has bitten you, now is the time to speak up!  :)

--
~Ethan~

Back to comp.lang.python | Previous | Next — Next in thread | Find similar | Unroll thread


Thread

Real-world use of Counter Ethan Furman <ethan@stoneleaf.us> - 2014-11-05 08:26 -0800
  Re: Real-world use of Counter Rustom Mody <rustompmody@gmail.com> - 2014-11-05 22:00 -0800
    Re: Real-world use of Counter Ian Kelly <ian.g.kelly@gmail.com> - 2014-11-06 08:51 -0700
    Re: Real-world use of Counter Ian Kelly <ian.g.kelly@gmail.com> - 2014-11-06 22:05 -0700

csiph-web