inner function accessing outer function's variable
This question already has an answer here:
the variable of the outer function is available only when that function is running on, ie it vanishes when my_outer_function ends.
That's not entirely true. The variable is available in my_outer_function
's scope. my_inner_function
has the scope of its own declarations and its parent's scope.
my_inner_function
references a variable outside of its own scope, and so those references are bound to my_inner_function
as a closure when its parent's scope is no longer available. To learn more about closures, see Can you explain closures (as they relate to Python)? (taken from Paul Rooney's comment)
上一篇: 为什么我们不用python关闭?
下一篇: 内部函数访问外部函数的变量