嵌套函数中的python变量作用域 [英] python variable scope in nested functions

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

问题描述

我正在阅读这篇文章关于装饰者。



第8步中,有一个函数定义为:

 def outer():
x = 1
def inner():
print x#1
return inner

如果我们运行它:

 >>> foo = outer()
>>> foo.func_closure#doctest:+ ELLIPSIS

它不打印x。根据解释:


一切都按照Python的范围规则工作 - x是我们函数外部的一个本地
变量。当内部打印点x#1时,Python
寻找一个局部变量到内部,并且不会在
封闭范围内寻找内部函数,找到它。



但从变寿命的角度来看呢?我们的
变量x对函数outer来说是本地的,这意味着只有当函数outer在运行时它才存在
。根据我们的Python
模型的工作原理,我们无法在outer之后调用inner,直到
,所以当我们调用inner时,x不应该再存在了,也许
a应该会出现某种类型的运行时错误。


然而,我不太明白第二段的意思。



我明白inner()会得到x的值,但为什么它不会打印出来?

感谢

更新



感谢所有的答案。现在我明白了原因。
return inner 仅仅是一个指向inner()的指针<但是它不会被执行,这就是为什么inner()不会打印x因为它完全没有被调用

解决方案

你不是调用 inner 。您已经调用 outer ,它会返回 inner ,但不调用它。如果你想调用 inner ,执行 foo()(因为你把 ()到名称 foo )。



您引用的段落是排序这个问题的切线。你说你已经理解为什么 inner 得到 x 的值,这就是段落解释的内容。基本上,如果在嵌套函数中使用局部变量,并且返回嵌套函数,则即使该变量定义的作用域不再处于活动状态,变量的值也会与返回的函数一起存储。通常 x 会在 outer 完成后消失,因为 x outer 来说只是本地的。但 outer 返回 inner ,仍然需要访问 x 。因此 x 会被封装到所谓的闭包中,因此稍后仍可以通过 inner 访问。


I am reading this article about decorator.

At Step 8 , there is a function defined as:

def outer():
    x = 1
    def inner():
       print x # 1
    return inner

and if we run it by:

>>> foo = outer()
>>> foo.func_closure # doctest: +ELLIPSIS

it doesn't print x. According to the explanation :

Everything works according to Python’s scoping rules - x is a local variable in our function outer. When inner prints x at point #1 Python looks for a local variable to inner and not finding it looks in the enclosing scope which is the function outer, finding it there.

But what about things from the point of view of variable lifetime? Our variable x is local to the function outer which means it only exists while the function outer is running. We aren’t able to call inner till after the return of outer so according to our model of how Python works, x shouldn’t exist anymore by the time we call inner and perhaps a runtime error of some kind should occur.

However, I don't really understand what the second paragraph means.

I understand inner() does get the value of x but why it doesn't print x out?

thanks

UPDATE:

Thanks all for the answers. Now I understand the reason. the "return inner" is just a pointer to inner() but it doesn't get executed, that is why inner() doesn't print x as it is not called at all

解决方案

You are not calling inner. You have called outer, which returns inner, but without calling it. If you want to call inner, do foo() (since you assinged the result of outer() to the name foo).

The paragraph you quoted is sort of tangential to this issue. You say you already understand why inner gets the value of x, which is what that paragraph is explaining. Basically, if a local variable is used in a nested function, and that nested function is returned, the value of the variable is stored along with the returned function, even if the scope where that variable was defined in no longer active. Normally x would be gone after outer finished, because x is just local to outer. But outer returns inner, which still needs access to x. So x gets wrapped up into what's called a closure, so it can still be accessed by inner later on.

这篇关于嵌套函数中的python变量作用域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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