什么是“lambda 绑定"?在 Python 中? [英] What is "lambda binding" in Python?

查看:38
本文介绍了什么是“lambda 绑定"?在 Python 中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我了解 Python 中的 lambda 函数是什么,但我无法通过搜索 Python 文档找到lambda 绑定"的含义.一个阅读它的链接会很棒.一个简单的解释例子会更好.谢谢.

I understand what are lambda functions in Python, but I can't find what is the meaning of "lambda binding" by searching the Python docs. A link to read about it would be great. A trivial explained example would be even better. Thank you.

推荐答案

一、一般定义:

当程序或函数语句被执行,当前值形式参数被保存(在堆栈)并在范围内声明,他们被绑定到实际参数的值电话.当声明是退出,那些的原始值恢复正式论点.这协议是完全递归的.如果在声明的主体,某事是done 导致形式参数再次绑定到新的价值观,lambda 绑定方案保证这一切都会有条不紊地发生方式.

When a program or function statement is executed, the current values of formal parameters are saved (on the stack) and within the scope of the statement, they are bound to the values of the actual arguments made in the call. When the statement is exited, the original values of those formal arguments are restored. This protocol is fully recursive. If within the body of a statement, something is done that causes the formal parameters to be bound again, to new values, the lambda-binding scheme guarantees that this will all happen in an orderly manner.

现在,这里的讨论中有一个很好的 python 示例:

Now, there is an excellent python example in a discussion here:

"...x 只有一个绑定:执行 x = 7 只会改变预先存在的绑定中的值.这就是为什么

"...there is only one binding for x: doing x = 7 just changes the value in the pre-existing binding. That's why

def foo(x): 
   a = lambda: x 
   x = 7 
   b = lambda: x 
   return a,b

返回两个都返回 7 的函数;如果在 x = 7 之后有一个新的绑定,函数会返回不同的值 [当然,假设你不调用 foo(7).还假设nested_scopes]...."

returns two functions that both return 7; if there was a new binding after the x = 7, the functions would return different values [assuming you don't call foo(7), of course. Also assuming nested_scopes]...."

这篇关于什么是“lambda 绑定"?在 Python 中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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