Python Glossary Русский

Python 3.4+Exceptionsexception

statistics.StatisticsError

module exceptions

Invalid data for a statistics function, e.g. statistics.mean([]).

Syntax

raise statistics.StatisticsError

Behind it there is nearly always empty or too short data: mean and median need at least one value, variance and stdev need at least two. Checking the length before the call is usually cleaner than catching the error afterwards. It subclasses ValueError, so a plain except ValueError will catch it — along with every other value error you did not mean to hide.

Examples

import statistics
try:
    raise statistics.StatisticsError
except statistics.StatisticsError as e:
    print(type(e).__name__)   # → StatisticsError

See also

Open in the full glossarydocs.python.orgReport an inaccuracy