Variable arguments to a Python function

Jordan Krushen jordan at krushen.com
Thu May 29 00:49:22 EDT 2003


On Thu, 29 May 2003 00:22:11 -0400, Mahboob <mahboob at bellsouth.net> wrote:

> 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;
> }

Try this:

>>> def mult(*args):
...     result = 1
...     for x in args:
...             result *= x
...     return result
...
>>> mult(3, 3, 3)
27
>>> mult('a', 5)
'aaaaa'
>>> mult('a', 5, 6)
'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'

J.




More information about the Python-list mailing list