[issue39094] Add a default to statistics.mean and related functions

Raymond Hettinger report at bugs.python.org
Fri Dec 20 00:48:45 EST 2019


Raymond Hettinger <raymond.hettinger at gmail.com> added the comment:

Thought experiment: Suppose someone proposed, "math.log(x) should take an optional default argument because it is inconvenient to a catch a ValueError if the input is non-positive".   Or more generally, what if someone proposed, "every function in Python that can raise a ValueError should offer a default argument."  One could imagine a use case for both of these proposals but that doesn't mean that the API extensions would be warranted.

Also, ISTM the analogy to min() and max() is imperfect.  Those aren't descriptive statistics.  For min() and max() we can know a priori that a probability is never lower than 0.0 or greater than 1.0 for example.

Lastly, in common cases where the input is a sequence (rather than just an iterator), we already have a ternary operator to does the job nicely:

   central_value = mean(data) if data else 'unknown'

For the less common case, a try/except is not an undue burden; after all, it is a basic core language feature.

----------

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue39094>
_______________________________________


More information about the Python-bugs-list mailing list