variable function call

Sean 'Shaleh' Perry shalehperry at home.com
Thu Nov 8 11:15:02 EST 2001


On 08-Nov-2001 Uwe Schmitt wrote:
> Hi,
> 
> I'd like to write a wrapper-function which takes a function and a
> variable parameter-list like this:
> 
> 
>     def wrapper(fun, *args):
> 
>       ... do some checks ...
> 
>       fun(args)
> 
>       ... do some update of states ...
> 
> 
> But if I use the function
> 
> 
>    def sum(a,b): return a+b
> 
> 
> and do
> 
>    wrapper(sum,1,2)
> 
> I get an errormessage "sum() takes two arguments ( 1 given) " because
> sum() gets one tuple of two values instead of two seperate values.
> 
> Is there any trick to circumvent this error ??? The problem is, that
> I'm not able to manipulate the calling-syntax of the functions
> transmitted to the wrapper...
> 

you want to use the function 'apply' here.  The syntax is simple, 'apply(func,
args, kwargs)' args is the argument tuple and the kwargs is optional.




More information about the Python-list mailing list