[Tutor] apply help (newbie)
Sean 'Shaleh' Perry
shalehperry@attbi.com
Sat, 15 Dec 2001 09:24:56 -0800 (PST)
On 15-Dec-2001 Sean 'Shaleh' Perry wrote:
>>>> def call(func, *args):
> ... print type(*args)
> ... apply(func, args)
> ...
>>>> def foo(a,b,c):
> ... print a, b, c
> ...
>>>> call(foo, 1, 2, 3)
> Traceback (most recent call last):
> File "<stdin>", line 1, in ?
> File "<stdin>", line 2, in call
> TypeError: type() takes exactly 1 argument (3 given)
>
> *args is python magic for 'expand me in place'.
>
>>> def call(func, *args, **kwargs):
... print len(args)
... print len(kwargs)
... apply(func, args, kwargs)
...
>>> call(foo, a=1, c=3, b=2)
0
3
1 2 3
Just wanted to show you the keyword args syntax while we were discussing nifty
ways to call functions.