Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > function that accepts any amount of arguments?

Reply
Thread Tools

function that accepts any amount of arguments?

 
 
globalrev
Guest
Posts: n/a
 
      04-24-2008
if i want a function that can take any amount of arguments how do i
do?

lets say i want a function average that accepts any number of integers
and returns the average.
 
Reply With Quote
 
 
 
 
Paul McNett
Guest
Posts: n/a
 
      04-24-2008
globalrev wrote:
> if i want a function that can take any amount of arguments how do i
> do?


Put an asterisk before the argument name.


> lets say i want a function average that accepts any number of integers
> and returns the average.


def avg(*args):
return sum(args) / len(args)

There are some dangers (at least two glaring ones) with this code,
though, which I leave as an exercise for the reader.



Paul
 
Reply With Quote
 
 
 
 
Steve Holden
Guest
Posts: n/a
 
      04-24-2008
globalrev wrote:
> if i want a function that can take any amount of arguments how do i
> do?
>
> lets say i want a function average that accepts any number of integers
> and returns the average.


Use a parameter of the form *args - the asterisk tells the interpreter
to collect positional arguments into a tuple. Untested:

def mean(*x):
total = 0.0
for v in x:
total += v
return v/len(x)

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

 
Reply With Quote
 
Terry Reedy
Guest
Posts: n/a
 
      04-24-2008

"globalrev" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
| if i want a function that can take any amount of arguments how do i
| do?
|
| lets say i want a function average that accepts any number of integers
| and returns the average.

To add to the other comments, read the ref manual section of function defs.



 
Reply With Quote
 
Steve Holden
Guest
Posts: n/a
 
      04-24-2008
Ken wrote:
> "Steve Holden" <(E-Mail Removed)> 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

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

 
Reply With Quote
 
Bruno Desthuilliers
Guest
Posts: n/a
 
      04-24-2008
Paul McNett a écrit :
>
> def avg(*args):
> return sum(args) / len(args)
>
> There are some dangers (at least two glaring ones) with this code,
> though, which I leave as an exercise for the reader.


try:
avg("toto", 42)
except TypeError, e:
print "this is the first one : %s" % e
try:
avg()
except ZeroDivisionError, e:
print "this is the second : %s" % e

As far as I'm concerned, I would not handle the first one in the avg
function - just document that avg expects numeric args.

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).
 
Reply With Quote
 
malkarouri
Guest
Posts: n/a
 
      04-24-2008
On Apr 24, 12:43*pm, Bruno Desthuilliers <bruno.
(E-Mail Removed)> 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)?

k
 
Reply With Quote
 
Jonathan Gardner
Guest
Posts: n/a
 
      04-24-2008
On Apr 24, 5:28*am, malkarouri <(E-Mail Removed)> 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.


 
Reply With Quote
 
Steve Holden
Guest
Posts: n/a
 
      04-24-2008
Jonathan Gardner wrote:
> On Apr 24, 5:28 am, malkarouri <(E-Mail Removed)> 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.
>


This is Advice. Software Engineering's next door

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

 
Reply With Quote
 
bruno.desthuilliers@gmail.com
Guest
Posts: n/a
 
      04-24-2008
On 24 avr, 14:28, malkarouri <(E-Mail Removed)> wrote:
> On Apr 24, 12:43 pm, Bruno Desthuilliers <(E-Mail Removed)> 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).

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
problem in running a basic code in python 3.3.0 that includes HTML file Satabdi Mukherjee Python 1 04-04-2013 07:48 PM
Is there a min function that accepts any number of arguments? Peng Yu C++ 12 09-07-2008 05:22 AM
501 PIX "deny any any" "allow any any" Any Anybody? Networking Student Cisco 4 11-16-2006 10:40 PM
Making a class that accepts a user-defined STL coparison function titancipher C++ 4 06-13-2005 07:51 AM
How do I write a function that accepts a 'printf' type parameter? winbatch C++ 5 02-15-2005 04:34 AM



Advertisments