Variable arguments to a Python function

Ganesan R rganesan at myrealbox.com
Thu May 29 00:46:01 EDT 2003


>>>>> "Mahboob" == Mahboob  <mahboob at bellsouth.net> writes:

> How can I pass variable number of arguments to a Python function and access
> them inside the function?

> In Perl, you could write

> sub all_of {
>     my $result = 1;
>     while ( @_) {
>         $result *= shift;
>        }
>     return result;
> }
> (From Mastering Algorithms with Perl, pg 569)
> You can compute the product of any number of variables with the above
> function.

Literal translation:

def all_of(*args):
    result = 1
    for i in args:
        result *= i
    return

*args gives you the rest of the (non-keyword) arguments to the function 
as a tuple. 

Ganesan

-- 
Ganesan R (rganesan at debian dot org) | http://www.debian.org/~rganesan/
1024D/5D8C12EA, fingerprint F361 84F1 8D82 32E7 1832  6798 15E0 02BA 5D8C 12EA




More information about the Python-list mailing list