Yes. By including the function call in your input argument/s, you can call two (or more) functions at once.
In mathematics, an argument of a function is a value provided to obtain the function's result. It is also called an independent variable. , is called a unary function. A function of two or more variables is considered to have a domain consisting of ordered pairs or tuples of argument values.
Functions can be passed into other functionsFunctions, like any other object, can be passed as an argument to another function.
Function Call When calling a function with a function parameter, the value passed must be a pointer to a function. Use the function's name (without parentheses) for this: func(print); would call func , passing the print function to it.
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
>>>
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.
Functions in Python are first-class objects. But your function definition is a bit off.
def myfunc(anotherfunc, extraArgs, extraKwArgs):
return anotherfunc(*extraArgs, **extraKwArgs)
Sure, that is why python implements the following methods where the first parameter is a function:
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.
anotherfunc(*extraArgs)
For example:
def anotherfunc(inputarg1, inputarg2):
pass
def myfunc(func = anotherfunc):
print func
When you call myfunc, you do this:
myfunc(anotherfunc(inputarg1, inputarg2))
This will print the return value of anotherfunc.
Hope this helps!
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