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

时间:2022-01-15 20:08:21

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 funcM as 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函数是什么)。

2 个解决方案

#1


11  

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 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. 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更受限制。 Lambdas只能包含表达式,而不能包含语句。表达式是你可以放在=赋值右侧的任何东西。 (如果你想变得更迂腐,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,如果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.

最后,总是做一些导致最易读和可维护的代码。就性能而言,lambda和普通函数之间没有区别。

#2


8  

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函数?

#1


11  

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 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. 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更受限制。 Lambdas只能包含表达式,而不能包含语句。表达式是你可以放在=赋值右侧的任何东西。 (如果你想变得更迂腐,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,如果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.

最后,总是做一些导致最易读和可维护的代码。就性能而言,lambda和普通函数之间没有区别。

#2


8  

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函数?