Python function as a function argument?
Solution 1
Can a Python function be an argument of another function?
Yes.
def myfunc(anotherfunc, extraArgs):
anotherfunc(*extraArgs)
To be more specific ... with various arguments ...
>>> def x(a,b):
... print "param 1 %s param 2 %s"%(a,b)
...
>>> def y(z,t):
... z(*t)
...
>>> y(x,("hello","manuel"))
param 1 hello param 2 manuel
>>>
Solution 2
Here's another way using *args
(and also optionally), **kwargs
:
def a(x, y):
print x, y
def b(other, function, *args, **kwargs):
function(*args, **kwargs)
print other
b('world', a, 'hello', 'dude')
Output
hello dude
world
Note that function
, *args
, **kwargs
have to be in that order and have to be the last arguments to the function calling the function.
Solution 3
Functions in Python are first-class objects. But your function definition is a bit off.
def myfunc(anotherfunc, extraArgs, extraKwArgs):
return anotherfunc(*extraArgs, **extraKwArgs)
Solution 4
Sure, that is why python implements the following methods where the first parameter is a function:
- map(function, iterable, ...) - Apply function to every item of iterable and return a list of the results.
- filter(function, iterable) - Construct a list from those elements of iterable for which function returns true.
- reduce(function, iterable[,initializer]) - Apply function of two arguments cumulatively to the items of iterable, from left to right, so as to reduce the iterable to a single value.
- lambdas
Solution 5
Function inside function: we can use the function as parameter too..
In other words, we can say an output of a function is also a reference for an object, see below how the output of inner function is referencing to the outside function like below..
def out_func(a):
def in_func(b):
print(a + b + b + 3)
return in_func
obj = out_func(1)
print(obj(5))
the result will be.. 14
Hope this helps.
a7664
Updated on January 03, 2020Comments
-
a7664 over 4 years
Can a Python function be an argument of another function?
Say:
def myfunc(anotherfunc, extraArgs): # run anotherfunc and also pass the values from extraArgs to it pass
So this is basically two questions:
- Is it allowed at all?
- And if it is, how do I use the function inside the other function? Would I need to use exec(), eval() or something like that? Never needed to mess with them.
BTW, extraArgs is a list/tuple of anotherfunc's arguments.
-
alseether over 6 years
-
Aysennoussi about 9 yearscan extraArgs be a function as well ? if so how do you call it ?
-
Manuel Salvadores about 9 years@sekai Yes, extraArgs can be a function too.
-
Pipo over 6 yearsMore infos about *args & **kwargs can be found here pythontips.com/2013/08/04/args-and-kwargs-in-python-explained
-
HMD about 6 yearsPlease consider adding some explanation to your answer.
-
4dc0 about 5 yearswhere is this documented?
-
Bob Bobster almost 5 yearsYou are not passing function as an argument here, instead the return value.
-
Karl Knechtel about 2 years@4dc0 I don't think it's directly documented; it's just a natural consequence of how Python's data model works. Everything that can be bound to a name is an object; objects can be passed to functions; functions can be bound to a name (that's what happens when you use
def
); therefore, functions can be passed to functions. That said, you may be interested in docs.python.org/3.11/howto/functional.html, which shows elementary techniques that make use of this reality, and shows off some of the standard library goodies designed to capitalize on it. -
Karl Knechtel about 2 yearsSee also: docs.python.org/3.11/howto/functional.html. The builtins also include
sort
,min
andmax
which accept akey
keyword argument that is expected to be a callable. -
Karl Knechtel about 2 yearsSee also: stackoverflow.com/questions/739654/…