functions with unlimeted variable arguments...

John Machin sjmachin at lexicon.net
Sat Jun 18 09:42:47 CEST 2005


Xah Lee wrote:
> how can i define a function with variable parameters? For example,
> 
> f(a) would return [a]
> f(a,b) would return [a,b]
> f(a,b,...) would return [a,b,...]
> 
> One solution is of course to make the argument as a list. i.e.
> f([a,b,...])
> but are there other solutions?
> 
>>> def foo(*args):
...     print repr(args)
...     return list(args)
...
>>> foo()
()
[]
>>> foo(42)
(42,)
[42]
>>> foo(666, 'hello', 1.234)
(666, 'hello', 1.234)
[666, 'hello', 1.234]
>>>




More information about the Python-list mailing list