Variable arguments to a Python function

Cameron Laird claird at lairds.com
Fri May 30 09:35:32 EDT 2003


In article <ELlBa.22564$Io.1967908 at newsread2.prod.itd.earthlink.net>,
Joe Cheng <josephmcheng at hotmail.NO.SPAM.com> wrote:
>> import operator
>> def product(*sequence):
>>         return reduce(operator.mul, sequence)
>>
>> >>> product(1,2,3)
>> 6
>
>Nice!  And just for fun, without operator:
>
>def product(*sequence):
>    return reduce(lambda x, y: x*y, sequence)
>
>I'm a noob to python (from Java, C#) and it's refreshing to see such concise
>syntax...
>
>

Good discussion.

Perhaps there's value in mentioning, moreover, that
Python likely does a bit less varargs-ing than other
languages, because it's so natural to pass lists
(and other sequence types) around.  One might make
the design choice to pass a single list (or more!),
rather than a varargs-to-parse.
-- 

Cameron Laird <Cameron at Lairds.com>
Business:  http://www.Phaseit.net
Personal:  http://phaseit.net/claird/home.html




More information about the Python-list mailing list