On Apr 25, 2:12 am, "[EMAIL PROTECTED]" <[EMAIL PROTECTED]> wrote: > On 24 avr, 14:28, malkarouri <[EMAIL PROTECTED]> wrote: > > > On Apr 24, 12:43 pm, Bruno Desthuilliers <[EMAIL PROTECTED]> wrote: > > > [...] > > > > Not quite sure what's the best thing to do in the second case - raise a > > > ValueError if args is empty, or silently return 0.0 - but I'd tend to > > > choose the first solution (Python's Zen, verses 9-11). > > > What's wrong with raising ZeroDivisionError (not stopping the > > exception in the first place)? > > Because - from a semantic POV - the real error is not that you're > trying to divide zero by zero, but that you failed to pass any > argument. FWIW, I'd personnaly write avg as taking a sequence - ie, > not using varargs - in which case calling it without arguments would a > TypeError (so BTW please s/Value/Type/ in my previous post).
The problem with passing it as a sequence is, if you want to call it, you may have to wrestle with this odd looking code: avg((3, 4, 6, 7)) rather than this, more natural code: avg(3, 4, 6, 7) And FWIW, the OP asked if it is possible to pass variable amount of arguments, avg is just a mere example of one where it could be used not where it could be best used. Nick Craig-Wood wrote: > Steve Holden <[EMAIL PROTECTED]> wrote: >> Ken wrote: >>> "Steve Holden" <[EMAIL PROTECTED]> wrote in message >> [...] >>>> def mean(*x): >>>> total = 0.0 >>>> for v in x: >>>> total += v >>>> return v/len(x) >>> think you want total/len(x) in return statement >> Yes indeed, how glad I am I wrote "untested". I clearly wasn't pair >> programming when I wrote this post ;-) > Posting to comp.lang.python is pair programming with the entire > internet ;-) No, actually it's pair programming with the readers of c.l.py (or more accurately with the readers of c.l.py that happens to pass the said thread). -- http://mail.python.org/mailman/listinfo/python-list