问题:Python函数作为函数参数吗?
Python函数可以作为另一个函数的参数吗?
说:
def myfunc(anotherfunc, extraArgs):
# run anotherfunc and also pass the values from extraArgs to it
pass
所以这基本上是两个问题:
- 可以吗?
- 如果是的话,如何在其他函数中使用该函数?我需要使用exec(),eval()还是类似的东西?从来不需要与他们搞混。
顺便说一句,extraArgs是anotherfunc参数的列表/元组。
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.
回答 0
Python函数可以作为另一个函数的参数吗?
是。
def myfunc(anotherfunc, extraArgs):
anotherfunc(*extraArgs)
更具体地说…带有各种参数…
>>> 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
>>>
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
>>>
回答 1
这是使用*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')
输出量
hello dude
world
需要注意的是function
,*args
,**kwargs
必须按照这个顺序和必须的函数调用该函数的最后的参数。
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.
回答 2
Python中的函数是一流的对象。但是您的函数定义有点偏离。
def myfunc(anotherfunc, extraArgs, extraKwArgs):
return anotherfunc(*extraArgs, **extraKwArgs)
Functions in Python are first-class objects. But your function definition is a bit off.
def myfunc(anotherfunc, extraArgs, extraKwArgs):
return anotherfunc(*extraArgs, **extraKwArgs)
回答 3
当然,这就是python在第一个参数为函数的情况下实现以下方法的原因:
- map(function,iterable,…)-将函数应用于iterable的每个项目并返回结果列表。
- filter(function,iterable)-从这些iterable的元素构造一个列表,对于这些元素,函数将返回true。
- reduce(function,iterable [,initializer])-将两个参数的函数从左到右累计应用于iterable的项,以将iterable减少为单个值。
- Lambdas
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
回答 4
- 是的,允许。
- 您可以像使用其他函数一样使用该函数:
anotherfunc(*extraArgs)
- Yes, it’s allowed.
- You use the function as you would any other:
anotherfunc(*extraArgs)
回答 5
- 是。通过在输入参数中包含函数调用,可以一次调用两个(或多个)函数。
例如:
def anotherfunc(inputarg1, inputarg2):
pass
def myfunc(func = anotherfunc):
print func
调用myfunc时,请执行以下操作:
myfunc(anotherfunc(inputarg1, inputarg2))
这将打印anotherfunc的返回值。
希望这可以帮助!
- Yes. By including the function call in your input argument/s, you can call two (or more) functions at once.
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!
回答 6
函数内部的函数:我们也可以将函数用作参数。
换句话说,我们可以说函数的输出也是对象的引用,请参阅下文,内部函数的输出如何引用外部函数,如下所示。
def out_func(a):
def in_func(b):
print(a + b + b + 3)
return in_func
obj = out_func(1)
print(obj(5))
结果将是.. 14
希望这可以帮助。
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.
回答 7
def x(a):
print(a)
return a
def y(a):
return a
y(x(1))
def x(a):
print(a)
return a
def y(a):
return a
y(x(1))
回答 8
def x(a):
print(a)
return a
def y(func_to_run, a):
return func_to_run(a)
y(x, 1)
我认为这将是一个更适当的示例。现在我想知道的是,是否有一种方法可以编码要在提交给另一个函数的参数中使用的函数。我相信在C ++中,但是在Python中我不确定。
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. I believe there is in C++, but in Python I am not sure.
回答 9
装饰器在Python中非常强大,因为它允许程序员将函数作为参数传递,也可以在另一个函数中定义函数。
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()
输出量
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()
Output
- This is inside function before execution
- I am argument function