[英]Python function as a function argument?
Suppose I have some code like:假设我有一些代码,例如:
def myfunc(anotherfunc, extraArgs):
# somehow call `anotherfunc` here, passing it the `extraArgs`
pass
I want to pass another existing function as the anotherfunc
argument, and a list or tuple of arguments as extraArgs
, and have myfunc
call the passed-in function with those arguments. I want to pass another existing function as the anotherfunc
argument, and a list or tuple of arguments as extraArgs
, and have myfunc
call the passed-in function with those arguments.
Is this possible?这可能吗? How do I do it - would I need exec
/ eval
or similar?我该怎么做 - 我需要exec
/ eval
或类似的吗?
Can a Python function be an argument of another function? Python function 可以是另一个 function 的参数吗?
Yes.是的。
def myfunc(anotherfunc, extraArgs):
anotherfunc(*extraArgs)
To be more specific... with various arguments...更具体地说......与各种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
:这是使用*args
(也可以选择)的另一种方式, **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 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. 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. Python 中的函数是一等对象。 But your function definition is a bit off .但是您的 function 定义有点偏离。
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:当然,这就是 python 实现以下方法的原因,其中第一个参数是 function:
Function inside function: we can use the function as parameter too.. function 内部的 Function:我们也可以使用 function 作为参数。
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.. 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结果将是.. 14
Hope this helps.希望这可以帮助。
Decorators are very powerful in Python since it allows programmers to pass function as argument and can also define function inside another function. Decorators are very powerful in Python since it allows programmers to pass function as argument and can also define function inside another function.
def decorator(func):
def insideFunction():
print("This is inside function before execution")
func()
return insideFunction
def func():
print("I am argument function")
func_obj = decorator(func)
func_obj()
For example:例如:
def anotherfunc(inputarg1, inputarg2):
pass
def myfunc(func = anotherfunc):
print func
When you call myfunc, you do this:当你调用 myfunc 时,你会这样做:
myfunc(anotherfunc(inputarg1, inputarg2))
This will print the return value of anotherfunc.这将打印 anotherfunc 的返回值。
Hope this helps!希望这可以帮助!
anotherfunc(*extraArgs)
您可以像使用其他任何方法一样使用 function: anotherfunc(*extraArgs)
def x(a):
print(a)
return a
def y(a):
return a
y(x(1))
def x(a):
print(a)
return a
def y(func_to_run, a):
return func_to_run(a)
y(x, 1)
That I think would be a more proper sample.我认为这将是一个更合适的样本。 Now what I wonder is if there is a way to code the function to use within the argument submission to another function.现在我想知道是否有办法对 function 进行编码,以便在提交给另一个 function 的参数中使用。 I believe there is in C++, but in Python I am not sure.我相信在 C++ 中有,但在 Python 中我不确定。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.