简体   繁体   English

Python如何打印超出范围的变量

[英]How does Python print a variable that is out of scope

I have the following function in Python that seems to be working: 我在Python中有以下函数似乎正在工作:

def test(self):
    x = -1
    # why don't I need to initialize y = 0 here?
    if (x < 0):
        y = 23

    return y

But for this to work why don't I need to initialize variable y? 但为了这个工作,为什么我不需要初始化变量y? I thought Python had block scope so how is this possible? 我以为Python有块范围所以这怎么可能?

This appears to be a simple misunderstanding about scope in Python . 这似乎是对Python范围的一个简单误解。 Conditional statements don't create a scope. 条件语句不创建范围。 The name y is in the local scope inside the function, because of this statement which is present in the syntax tree: 名称y位于函数内的本地范围内,因为语法树中存在此语句:

y = 23

This is determined at function definition time, when the function is parsed. 这是在解析函数时在函数定义时确定的。 The fact that the name y might be used whilst unbound at runtime is irrelevant. 名称y可能在运行时未绑定时使用这一事实无关紧要。

Here's a simpler example highlighting the same issue: 这是一个突出显示相同问题的简单示例:

>>> def foo():
...     return y
...     y = 23
... 
>>> def bar():
...     return y
... 
>>> foo.func_code.co_varnames
('y',)
>>> bar.func_code.co_varnames
()
>>> foo()
# UnboundLocalError: local variable 'y' referenced before assignment
>>> bar()
# NameError: global name 'y' is not defined

It seems that you misunderstood this part of Python's documentation : 您似乎误解了Python文档的这一部分:

A Python program is constructed from code blocks. Python程序由代码块构成。 A block is a piece of Python program text that is executed as a unit. 块是一段Python程序文本,作为一个单元执行。 The following are blocks: a module, a function body, and a class definition. 以下是块:模块,函数体和类定义。
... ...
A scope defines the visibility of a name within a block. 范围定义块内名称的可见性。 If a local variable is defined in a block, its scope includes that block. 如果在块中定义了局部变量,则其范围包括该块。

So in this case block is something completely different from visual blocks of your code. 因此,在这种情况下,块与代码的可视块完全不同 Thereby if , for , while statements doesn't have their own scopes. 因此, ifforwhile语句没有自己的范围。 But it is worth noting that comprehensions and generator expressions are implemented using a function scope , so they have their own scopes. 但值得注意的是, 使用函数范围实现理解和生成器表达式 ,因此它们具有自己的范围。

There is actually no block scope in python. python中实际上没有块范围。 Variables may be local (inside of a function) or global (same for the whole scope of the program). 变量可以是局部的(函数内部)或全局的(对于整个程序范围也是如此)。

Once you've defined the variable y inside the 'if' block its value is kept for this specific function until you specifically delete it using the 'del' command, or the function exits. 一旦你在'if'块中定义了变量y,就会为这个特定的函数保留它的值,直到你使用'del'命令专门删除它,或者函数退出。 From the moment y is defined in the function, it is a local variable of this function. 从函数中定义y的那一刻起,它就是该函数的局部变量。

As in What's the scope of a Python variable declared in an if statement? 如在if语句中声明的Python变量的范围是什么? : "Python variables are scoped to the innermost function or module; control blocks like if and while blocks don't count." :“Python变量的范围限定在最里面的函数或模块中;控制块如if和while块不计算。”

Also useful: Short Description of the Scoping Rules? 还有用: 范围规则的简短描述?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM