Variable arguments to a Python function
Peter Abel
p-abel at t-online.de
Fri May 30 16:11:21 EDT 2003
"Joe Cheng" <josephmcheng at hotmail.NO.SPAM.com> wrote in message news:<ELlBa.22564$Io.1967908 at newsread2.prod.itd.earthlink.net>...
> > 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)
Nice too!? And just for fun, without operator and without def:
>>> product = lambda *seq:reduce(lambda x,y:x*y,seq)
>>> product(1,2,3)
6
Because varargs work with lambdas too.
>
> I'm a noob to python (from Java, C#) and it's refreshing to see such concise
> syntax...
Regards
Peter
More information about the Python-list
mailing list