function that accepts any amount of arguments?

Jonathan Gardner jgardner at jonathangardner.net
Thu Apr 24 19:12:59 CEST 2008


On Apr 24, 5:28 am, malkarouri <malkaro... at gmail.com> 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.





More information about the Python-list mailing list