member thudfoo wrote:
On 4/24/08, Jonathan Gardner <[EMAIL PROTECTED]> wrote:
On Apr 24, 5:28 am, malkarouri <[EMAIL PROTECTED]> wrote:
 >
 > What's wrong with raising ZeroDivisionError (not stopping the
 > exception in the first place)?
 >


Because when I use your module, call avg (or mean) without args, I
 should see an error that says, "Hey, you have to pass at least one
 value in!"

 ZeroDivisonError doesn't mean that. It means I tried to divide by
 zero. Naively, I don't see where I was dividing by zero (because I
 don't remember how to calculate the mean---that's what your code was
 for.)

 ValueError does mean that I didn't pass the right kind of arguments
 in. ValueError("No items specified") would be even clearer. (Or maybe
 TypeError?)

 In general, any exception thrown should be meaningful to the code you
 are throwing it to. That means they aren't familiar with how your code
 works.


[source]|557> def average(n, *ints):
        |...>     return (sum(ints)+n) / (len(ints) + 1)
        |...>
[source]|558> average (1,2,3)
        <558> 2
[source]|559> average(3)
        <559> 3
[source]|560> average(1,2)
        <560> 1
[source]|561> average(0)
        <561> 0
[source]|562> average()
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)

/usr/share/doc/packages/python-dateutil/source/<ipython console> in <module>()

TypeError: average() takes at least 1 argument (0 given)
--
http://mail.python.org/mailman/listinfo/python-list

It would also be usual to use floating arithmetic to ensure that the mean of 1 and 2 was 1.5 rather than 1.

regards
 Steve
--
Steve Holden        +1 571 484 6266   +1 800 494 3119
Holden Web LLC              http://www.holdenweb.com/

--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to