[Python-ideas] Pre-PEP: adding a statistics module to Python
Steven D'Aprano
steve at pearwood.info
Sun Aug 4 04:16:50 CEST 2013
On 04/08/13 11:51, Steven D'Aprano wrote:
> I don't know enough C to volunteer to do that. If the built-in sum() is improved to the point it passes my unit tests, I would consider using it in the future.
Actually, on further thought, I don't think I would. A statistic sum should be restricted to only operate on numbers, not on arbitrary non-numeric values that happen to support the + operator (e.g. lists). As far as I know, the *only* stats function that is defined to work with non-numeric data is mode() (which my code supports). So even if the built-in was improved, I'd still need to wrap it with something vaguely like this:
def sum(data, start=0):
if not isinstance(start, numbers.Number):
raise ...
result = builtins.sum(data, sum)
if not isinstance(result, numbers.Number):
raise ...
return result
(In hindsight, the decision to allow built-in sum to support non-numbers seems more and more unfortunate to me.)
Being able to add numbers, and get a nice error if a non-numeric type slips into your data, is part of the API for statistics libraries. Built-in sum() doesn't meet that requirement. That they happen to have the same name is neither here nor there. That's why we don't force everything into one giant flat namespace.
--
Steven
More information about the Python-ideas
mailing list