Re: Problems passing a function and its args around.

Guido.van.Rossum@cwi.nl
Tue, 14 Mar 1995 14:31:34 +0100

> I need to set up a function that takes 2 parameters - a function
> object, and a tuple of parameters for the function. I would then like the
> passed function to be called, with the parameters.
>
> To explain:
> >>> def test(a,b,c):
> ... print "a=%s, b=%s, c=%s" % (a,b,c)
> ...
> >>> def CallPasser( fn, args ):
> ... print "Args are ", args
> ... fn (args)
> ...
> >>> CallPasser(test, ('a','b','ccc'))
> What I am hoping to get out of the above statement is for the function
> test() to be called, as if "test('a','b','ccc')" had been typed at the
> prompt.

Use apply(fn, args).

--Guido van Rossum, CWI, Amsterdam <mailto:Guido.van.Rossum@cwi.nl>
<http://www.cwi.nl/cwi/people/Guido.van.Rossum.html>