在python中传递函数作为参数 [英] passing a function as an argument in python

查看:152
本文介绍了在python中传递函数作为参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我要计算以下f(f(...f(x)..).
基本上是其自身的许多功能.
目前,我正在执行以下操作以实现此结果(并返回所有中间步骤):

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):

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

然后将函数funcM作为参数传递:
print iterator(funcM, 100, 5).

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

这种方法没有错,计算正确.

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

但是有一种方法可以在不定义函数funcM的情况下进行此操作吗?
可能会将lambda函数作为参数传递给迭代器函数(抱歉,如果没有意义,我真的不知道什么是lambda函数).

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).

推荐答案

lambda函数(或更准确地说,是lambda expression )只是您可以在现场定义的函数,对在需要的地方.例如,

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,

f = lambda x: x * 2

完全相同

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表达式很有用,因为创建一个不是语句,这意味着,正如其他人已经回答的那样,您可以做

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

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

精确地获得想要的东西.

to get precisely what you want.

但是,lambda表达式和正则函数之间的主要区别是lambda受到更多限制. Lambda只能包含表达式,不能包含语句.表达式是您可以放在=赋值右侧的任何内容. (如果您想获得更多的知识,Python会将表达式定义为 http://docs.python .org/2/reference/expressions.html )

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表达式赋给变量(实际上,除了其参数之外,它根本不能具有局部变量).它无法打印(除非它调用了另一个函数).它不能具有for循环,while循环,if测试(三元运算符x if cond else y除外)或try/except块.

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,请三思而后行.如果使用常规函数,代码会不会更具可读性?您不想在代码中的其他地方重用该lambda表达式吗?

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和正常函数之间没有区别.

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.

这篇关于在python中传递函数作为参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆