In programming languages, a closure(also lexical closure or function closure) is a technique for implementing lexically scoped name binding in a language with first-class functions. Operationally, a closure is a record storing a function together with an environment. The environment is a mapping associating each free variable of the function (variables that are used locally, but defined in an enclosing scope) with the value or reference to which the name was bound when the closure was created. A closure (unlike a plain function) allows the function to access those captured variables through the closure’s copies of their values or references, even when the function is invoked outside their scope.

1. yz 是函数 closure_def 作用域下的局部变量
2. 函数 fn 定义在 函数 closure_def 的内部，同时它将直接调用 yz
3. 函数 closure_def返回的是函数 fn 的引用

• .__closure__ 返回一组存有变量值的cell对象
• .__closure__[0].cell_centents 将获取相应变量的值

----- For reprint please indicate the source -----
0%