[Tutor] How to handle exceptions raised inside a function?

Richard D. Moores rdmoores at gmail.com
Wed Dec 1 21:08:17 CET 2010


On Tue, Nov 30, 2010 at 13:23, Steven D'Aprano <steve at pearwood.info> wrote:
> Richard D. Moores wrote:
>>
>> Please take a look at 2 functions I just wrote to calculate the
>> harmonic and geometric means of lists of positive numbers:
>> <http://tutoree7.pastebin.com/VhUnZcma>.
>>
>> Both Hlist and Glist must contain only positive numbers, so I really
>> need to test for this inside each function. But is there a good way to
>> do this? What should the functions return should a non-positive number
>> be detected? Is there a conventional Pythonic way to do this?
>

> (2) If you don't trust that a sensible exception will be raised, then do
> your own error checking, and raise an exception.

I'll go with this one because I do want both Hlist and Glist to
contain only positive real numbers. So I'll go with Jerry Hill's
suggestion for both H and G. See the two revised functions at
<http://tutoree7.pastebin.com/VfYLpFQq>.

> For what it's worth, I have a module of statistics functions (shameless
> plug: http://pypi.python.org/pypi/stats and
> http://code.google.com/p/pycalcstats -- feedback and bug reports welcome)

An impressive collection. Thanks for sharing!

> that includes the harmonic and geometric mean. My harmonic mean looks like
> this:
>
> def harmonic_mean(data):
>    try:
>        m = mean(1.0/x for x in data)
>    except ZeroDivisionError:
>        return 0.0
>    if m == 0.0:
>        return math.copysign(float('inf'), m)
>    return 1/m

math.copysign! Didn't know about that one. But "mean"? It's not a
built-in function..

Dick

> Notice that if the data includes one or more zeroes, the harmonic mean
> itself will be zero: limit as x->0 of 1/x -> infinity, and 1/infinity -> 0.
> If the sum of reciprocals itself cancels to zero, I return the infinity with
> the appropriate sign. The only exceptions that could occur are:
>
> * mean will raise ValueError if the data is empty;
> * if an argument is non-numeric, TypeError will occur when I take the
> reciprocal of it.


More information about the Tutor mailing list