了解Lambda函数 [英] Understanding lambda functions

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

问题描述

我确实尝试阅读有关Lambda函数的信息,但没有遍历任何链接,该链接解释了有关其流程以及python解释器处理方式的一些问题,或者可能是我无法正确理解.我的问题很少,请有人可以为我澄清一下.这是代码:

Well I did try to read about Lambda functions but did not get across any link which explains few questions about its flow and the way it is handled by python interpretor or may be I could not understand it properly. I have few question, please can somebody clarify them for me. Here is the code :

def f1(n):
    print 'in f1, value is : ', n
    return lambda x: x+n

if __name__ == '__main__':
    f= f1(100)
    print f(1)
    print f(3)
    print f1(10)
    print f(5)
    print type(f1)

输出为:

in f1, value is :  100
101
103
in f1, value is :  10
<function <lambda> at 0x019C66B0>
105
<type 'function'>

我的问题是:

  • 对于 f(1) f(3)为什么不调用 print 语句以及何时调用我们称 f1(100)为什么不编译lambda语句.
  • 函数 f1 中的 x n 之间是什么关系.
  • 我认为 f(5)将为15(10 + 5)
  • 请说明打印f1(10)
  • 另外,请让我知道 lambda x:在这里的意思是,这个块的x名称是吗?请解释.
  • For f(1) and f(3) why print statement was not called and when we called f1(100) why lambda statement was not compiled.
  • What is the relation between x and n in function f1.
  • I thought f(5) will 15 ( 10 + 5)
  • Please explain print f1(10)
  • Also, please let me know what is lambda x: means here, is that x name of this block? please explain.

谢谢.

推荐答案

首先,您应该了解Python中的所有内容都是一个对象.而且功能确实可以实现所有功能.

First you should understand that everything in Python is an object. And functions do come in everything.

实际上,您是从函数 f1()返回对 lambda 函数的引用.您可以在任何变量中进行分配.

From your function f1(), you are actually returning a reference to a lambda function. Which you can assign in any variable.

例如:

>>> f = lambda x: x   # Assign reference to lambda function to f.
# Now you can call lambda function using `f`  
>>> f(1)
1


现在,让我们继续您的示例.您的第一个语句在 if

f = f1(100)

此分配等同于:

f = lambda x: x + 100  # Replace `n` with `100` in return statment in `f1`.

因此,接下来的两个调用非常简单:

So, the next two call is pretty straighforward:

print f(1)  # returns `1 + 100` = 101 (Replace `x` with `1`.
print f(3)  # returns `3 + 100` = 103

因此,现在您获得了 x n 之间的关系."x"被替换为 f 的参数,而"n"被替换为 f1 的参数.

So, now you get the relation between x and n. 'x' is replaced by the argument to f and 'n' is replaced by argument to f1.

请解释打印f1(10)

Please explain print f1(10)

f1()返回对lambda函数的引用.这就是它将打印的内容.输出类似于您在以下片段中获得的输出:

f1() returns a reference to a lambda function. And that is what it will print. The output is similar to the one which you get in the below snippet:

>>> def func():
        return 5

>>> func
<function func at 0x021F0F30>

由于用f1仅返回lambda函数,因此用 lambda 代替了 func .

Except that func is replaced with lambda, since f1 returns a lambda function only.

我认为f(5)将为15(10 + 5)

I thought f(5) will 15 ( 10 + 5)

如果您在上一条语句中将 f1 的返回值重新分配给 f ,您将获得该输出.但是,由于您刚刚打印了 f(10)的值,因此 f 仍绑定到- lambda x:x + 100 .这样您就会得到 105 .

You would have got that output, had you re-assigned the return value of f1 to f in the previous statement. But, since you have just printed the value of f(10), f is still binded to - lambda x: x + 100. So you get 105.

然后打印 f1 的类型,该类型仅是一个函数:

And then you print the type of f1, which is a function only:

print type(f1)

这篇关于了解Lambda函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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