Is it possible to pass functions with arguments to another function in Python?
Say for something like:
def perform(function): return function()
But the functions to be passed will have arguments like:
action1() action2(p) action3(p,r)
Higher Order FunctionsBecause functions are objects we can pass them as arguments to other functions. Functions that can accept other functions as arguments are also called higher-order functions. In the example below, a function greet is created which takes a function as an argument.
All functions in Python can be passed as an argument to another function (that just happens to be the sole purpose of lambda functions).
How about wrapping that function with another function in order to create a function scope that you can pre-include the argument value you want. Then call the wrapper function with the other args when you need to.
1. User-defined function. In Python, just like a normal variable, we can pass a user-defined function as an argument to another function. A function that accepts another function as its parameter is called a Higher-order function.
Do you mean this?
def perform(fun, *args): fun(*args) def action1(args): # something def action2(args): # something perform(action1) perform(action2, p) perform(action3, p, r)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With