variable function call 
Author Message
 variable function call

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...

Yours, Uwe

--

Universitaet des Saarlandes                     Angewandte Mathematik
Building 36.1 Room 4.17         PO-Box 151150   D-66041 Saarbruecken
Mobile:0177/6806587    Fax:+49(0)681/302-4435   Office:+49(0)681/302-2468



Mon, 26 Apr 2004 23:49:22 GMT  
 variable function call

Quote:

> 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)

        fun(*args)

HTH.  Python 2 or better (or was it 1.6?).

Cheers,
M.

--
  If you have too much free time and can't think of a better way to
  spend it than reading Slashdot, you need a hobby, a job, or both.
     -- http://www.cs.washington.edu/homes/klee/misc/slashdot.html#faq



Tue, 27 Apr 2004 00:15:58 GMT  
 variable function call


Quote:
> 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.


Tue, 27 Apr 2004 00:15:02 GMT  
 variable function call

Quote:
>>> Uwe Schmitt wrote
>     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)

Change
     fun(args)
to
     apply(fun, args)

Anthony



Tue, 27 Apr 2004 10:16:02 GMT  
 variable function call
Hi,

thanks a lot for your answers. I knew there is a method to solve
my probelm, but forgot how to do it.

Greetings, Uwe.

--

Universitaet des Saarlandes                     Angewandte Mathematik
Building 36.1 Room 4.17         PO-Box 151150   D-66041 Saarbruecken
Mobile:0177/6806587    Fax:+49(0)681/302-4435   Office:+49(0)681/302-2468



Tue, 27 Apr 2004 16:29:16 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. indirect function calls and variable variables

2. indirect function calls and variable variables

3. VC++ calling fortran function and fortran function calling a c++ function

4. How to find out name of calling function from called function

5. calling function recursively, local variables?

6. Local variables, call-back functions under Windows

7. evaluating a variable as a function call?

8. Function call or variable?

9. Function call or variable?

10. calling a variable from a separate function

11. Call a function using a variable

12. How update variable values by calling secondary functions ?

 

 
Powered by phpBB® Forum Software