function that accepts any amount of arguments?

Lie Lie.1296 at gmail.com
Sat Apr 26 02:59:09 EDT 2008


On Apr 25, 2:12 am, "bruno.desthuilli... at gmail.com"
<bruno.desthuilli... at gmail.com> wrote:
> On 24 avr, 14:28, malkarouri <malkaro... at gmail.com> wrote:
>
> > On Apr 24, 12:43 pm, Bruno Desthuilliers <bruno.42.desthuilli... at websiteburo.invalid> 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 <st... at holdenweb.com> wrote:
>>  Ken wrote:
>>> "Steve Holden" <st... at holdenweb.com> 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).



More information about the Python-list mailing list