There is a class Timer to which I am now sending a function in a similar way:

t = Timer(3.0, self.clPC4), def clPC4(self): ... 

How can I pass a function as an argument without creating it, for example in JavaScript, I would pass this function like this:

 timer(3.0, function() { ... }); 

Is there something similar in Python?

1 answer 1

Example:

 In [251]: def f(a, b, func): ...: return func(a,b) ...: In [252]: def my_mul(a,b): ...: return a * b ...: In [253]: f(3, 5, my_mul) Out[253]: 15 

 In [254]: def my_sum(a,b): ...: return a + b ...: In [255]: f(3, 5, my_sum) Out[255]: 8 
  • one
    Alternative to the second option using lambda: lambda a,b: a+b - floydya
  • @floydya, yes, you can use the lambda function, but I prefer to use them only when necessary ;-) - MaxU