简体   繁体   English

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

[英]passing a function as an argument in python

Suppose I want to calculate the following f(f(...f(x)..) . 假设我想计算以下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): 目前我正在做以下事情来实现这个结果(并返回所有中间步骤):

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 funcM as an argument: 然后我将函数funcM作为参数传递:
print iterator(funcM, 100, 5) . 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 ? 但是有没有办法在不定义函数funcM情况下做同样的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). 可能是将lambda函数作为参数传递给迭代器函数(对不起,如果它没有意义,我真的不知道lambda函数是什么)。

A lambda function (or more accurately, a lambda expression ) is simply a function you can define on-the-spot, right where you need it. lambda函数(或更准确地说,lambda 表达式 )只是一个您可以在现场定义的函数,就在您需要的地方。 For example, 例如,

f = lambda x: x * 2

is exactly the 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. 然而,lambda表达式和常规函数之间的主要区别在于lambda更受限制。 Lambdas can only contain expressions , not statements. Lambdas只能包含表达式 ,而不能包含语句。 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 ) (如果你想变得更迂腐,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). 这意味着lambda表达式无法赋值给变量(事实上,除了参数之外,它根本不能有局部变量)。 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. 它不能有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. 事实上,只要你认为你想使用lambda,请三思而后行。 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表达式是不是你想在代码中的其他地方重用?

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和普通函数之间没有区别。

Yes, you can just use lambda expressions. 是的,你可以使用lambda表达式。 They are made for this. 它们是为此而制造的。

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

Words from the docs : 来自文档的单词:

Lambdas are usually used to create small, anonymous functions. Lambdas通常用于创建小型匿名函数。 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表达式与您的函数完全相同,只是没有名称。

If you wish to learn more, Here is some good read: 如果您想了解更多信息,请阅读以下内容:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM