function that accepts any amount of arguments?

member thudfoo thudfoo at opensuse.us
Thu Apr 24 21:13:48 CEST 2008


On 4/24/08, Jonathan Gardner <jgardner at jonathangardner.net> wrote:
> 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.
>

[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)



More information about the Python-list mailing list