在python中将函数作为参数传递

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21035437/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 21:52:26  来源:igfitidea点击:

passing a function as an argument in python

pythonlambda

提问by Salvador Dali

Suppose I want to calculate the following f(f(...f(x)..).
Basically many times function of itself.
Currently I am doing the following to achieve this result (and to return all the intermediate steps):

假设我要计算以下内容f(f(...f(x)..)
基本上多次发挥其自身的作用。
目前我正在执行以下操作以实现此结果(并返回所有中间步骤):

def iterator(function, x, n=4):
    x = float(x)
    arr = []
    for i in range(n + 1):
        arr.append(x)
        x = function(x)

    return arr

def funcM(x):
    return x / 4 + 12

and then I am passing my function funcMas an argument:
print iterator(funcM, 100, 5).

然后我通过我的函数funcM作为参数:
print iterator(funcM, 100, 5)

There is nothing wrong with this approach, and calculations are correct.

这种方法没有任何问题,计算是正确的。

But is there a way to do the same without defining function funcM?
May be passing lambda function as an argument to iterator function (Sorry if it does not make sense, I do not really know what lambda functions are).

但是有没有办法在不定义函数的情况下做同样的事情funcM
可能将 lambda 函数作为参数传递给迭代器函数(对不起,如果它没有意义,我真的不知道 lambda 函数是什么)。

采纳答案by Max Noel

A lambda function (or more accurately, a lambda expression) is simply a function you can define on-the-spot, right where you need it. For example,

一个 lambda 函数(或者更准确地说,一个 lambda表达式)只是一个你可以在现场定义的函数,就在你需要的地方。例如,

f = lambda x: x * 2

is exactlythe same thing as

正是同样的事情,

def f(x):
    return x * 2

And when I say exactly, I mean it -- they disassemble to the same bytecode. The only difference between the two is that the one defined in the second example has a name.

当我确切地说,我的意思是——它们反汇编成相同的字节码。两者之间的唯一区别是第二个示例中定义的那个有一个名称。

Lambda expressions become useful because creating one is not a statement, which means that, as others have already answered, you can do

Lambda 表达式变得有用,因为创建一个不是一个语句,这意味着,正如其他人已经回答的那样,你可以做

print iterator(lambda x: x / 4 + 12, 100, 5)

to get precisely what you want.

得到你想要的。

The main difference between lambda expressions and regular functions, however, is that lambdas are more limited. Lambdas can only contain expressions, not statements. An expression is anything you can put on the right side of an =assignment. (if you want to get more pedantic, Python defines an expression as http://docs.python.org/2/reference/expressions.html)

然而,lambda 表达式和正则函数之间的主要区别在于,lambda 的局限性更大。Lambda 只能包含表达式,不能包含语句。表达式是您可以放在=作业右侧的任何内容。(如果你想变得更迂腐,Python 将表达式定义为http://docs.python.org/2/reference/expressions.html

What this means is a lambda expression can not assign to a variable (in fact, it can't have local variables at all, other than its parameters). It can't print (unless it calls another function that does). It can't have a for loop, a while loop, an if test (other than the ternary operator x if cond else y), or a try/except block.

这意味着 lambda 表达式不能分配给变量(实际上,除了它的参数之外,它根本不能有局部变量)。它不能打印(除非它调用另一个可以打印的函数)。它不能有 for 循环、while 循环、if 测试(三元运算符x if cond else y除外)或 try/except 块。

If you need to do any of those, just define a regular function. In fact, any time you think you want to use a lambda, think twice. Wouldn't the code be more readable if you used a regular function? Isn't that lambda expression something you'd like to reuse somewhere else in your code?

如果您需要执行其中任何一项,只需定义一个常规函数即可。事实上,任何时候你想使用 lambda 时,都要三思而后行。如果您使用常规函数,代码会不会更易读?那个 lambda 表达式是不是您想在代码中的其他地方重用?

In the end, always do what leads to the most readable and maintainable code. There is no difference between lambdas and normal functions as far as performance is concerned.

最后,始终做最易读和最可维护的代码。就性能而言,lambdas 和普通函数之间没有区别。

回答by aIKid

Yes, you can just use lambda expressions. They are made for this.

是的,您可以只使用 lambda 表达式。它们是为此而生的。

iterator(lambda x: x/4+12, 100, 5)

Words from the docs:

自言文档

Lambdas are usually used to create small, anonymous functions. Actually, they are just a syntatic sugar to define functions. The lambda expression above is exactly the same as your function, only without a name.

Lambda 通常用于创建小的匿名函数。实际上,它们只是定义函数的语法糖。上面的 lambda 表达式与您的函数完全相同,只是没有名称。

If you wish to learn more, Here is some good read:

如果你想了解更多,这里有一些很好的阅读:

http://www.diveintopython.net/power_of_introspection/lambda_functions.html
Why use lambda functions?

http://www.diveintopython.net/power_of_introspection/lambda_functions.html
为什么要使用 lambda 函数?