I only just started learning Python and found out that I can pass a function as the parameter of another function. Now if I call foo(bar())
it will not pass as a function pointer but the return value of the used function. Calling foo(bar)
will pass the function, but this way I am not able to pass any additional arguments. What if I want to pass a function pointer that calls bar(42)
?
I want the ability to repeat a function regardless of what arguments I have passed to it.
def repeat(function, times):
for calls in range(times):
function()
def foo(s):
print s
repeat(foo("test"), 4)
In this case the function foo("test")
is supposed to be called 4 times in a row.
Is there a way to accomplish this without having to pass "test" to repeat
instead of foo
?
While many of the answers here are good, this one might be helpful because it doesn't introduce any unnecessary repetition and the reason for callbacks in the first place is often to synchronize with other work outside of the main UI thread.
Enjoy!
You can either use a
lambda
:Or
functools.partial
:Or pass the arguments separately:
This final style is quite common in the standard library and major Python tools.
*args
denotes a variable number of arguments, so you can use this function asor
Note that I put the number of repetitions up front for convenience. It can't be the last argument if you want to use the
*args
construct.(For completeness, you could add keyword arguments as well with
**kwargs
.)You will need to pass the parameters for foo, to the repeat function: