Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing functions with arguments to another function in Python?

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) 
like image 228
Joan Venge Avatar asked Apr 29 '09 18:04

Joan Venge


People also ask

How do you pass an argument to another function in Python?

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.

Can a Python function take another 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 do you pass an argument from one function to another?

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.

Which standard Python functions accepts another function as a parameter?

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.


1 Answers

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) 
like image 173
S.Lott Avatar answered Sep 19 '22 18:09

S.Lott