[Python-ideas] statistics module in Python3.4

Nick Coghlan ncoghlan at gmail.com
Sun Feb 2 02:24:59 CET 2014


On 2 February 2014 09:24, Wolfgang Maier
<wolfgang.maier at biologie.uni-freiburg.de> wrote:
>> -----Ursprüngliche Nachricht-----
>> Von: Paul Moore [mailto:p.f.moore at gmail.com]
>> Gesendet: Sonntag, 2. Februar 2014 00:14
>> An: Wolfgang Maier
>> Cc: Python-Ideas
>> Betreff: Re: [Python-ideas] statistics module in Python3.4
>>
>> On 1 February 2014 22:00, Wolfgang Maier <wolfgang.maier at biologie.uni-
>> freiburg.de> wrote:
>> > It may also help to address this from the users' perspective. What
>> > possible other use-cases could there be to pass a Mapping (let alone a
>> > Counter) to one of the functions in statistics?
>>
>> Why not just pass counter.elements() to the functions if you want to use a
>> counter as a frequency table?
>>
>
> The difference is that by accepting a Counter directly functions like
> statistics._sum and mode can do their job faster and more space-
> efficient. Using counter.elements means exploding the data structure,
> then summing up (for _sum) all the values, while you could just sum
> all key*value of the Counter.

First make it correct, *then* make it fast. I'm inclined to favour the
approach of forcing the "iter()" call here, so Counter is always
treated the same as any other iterable.

There's a plausible case to be made for offering a more efficient
alternative API for working directly with collections.Counter objects
in the statistics module, but that should be considered specifically
for 3.5, rather than relying on an implicit detail of the current
implementation.

For a historical precedent, compare the printf-style formatting API,
which changes behaviour based on whether the RHS is a tuple, dict or
other type, and the newer format()/format_map() APIs where the special
case to handle passing in an existing dict without going through
**kwargs expansion was moved out to a suitably named dedicated method.

I have now filed two related issues for this:

3.4 release blocker to avoid inadvertently special casing Counter:
http://bugs.python.org/issue20478
3.5 RFE to provide tools to work directly with weight/frequency
mappings: http://bugs.python.org/issue20479

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia


More information about the Python-ideas mailing list