x = 1
def fn():
print x
fn()
This prints "1":
这打印“1”:
x = 1
def fn():
x += 1
print x
fn()
This raises "UnboundLocalError: local variable 'x' referenced before assignment"
这会引发“UnboundLocalError:赋值前引用的局部变量'x'”
What is going on here?
这里发生了什么?
4 个解决方案
#1
4
In Python, assigning to a variable is also an implicit local declaration, that is resolved during the bytecode compilation. So
在Python中,分配给变量也是一个隐式本地声明,在字节码编译期间解析。所以
x += 1
will create a local variable x
and compile to this byte code:
将创建一个局部变量x并编译为此字节代码:
0 LOAD_FAST 0 (x)
3 LOAD_CONST 1 (1)
6 INPLACE_ADD
7 STORE_FAST 0 (x)
The command LOAD_FAST
will try to load a local variable x
which is not yet defined, that's why it fails.
命令LOAD_FAST将尝试加载尚未定义的局部变量x,这就是失败的原因。
However, if you define x
as global
explicitly, then it will use LOAD_GLOBAL
/STORE_GLOBAL
instead.
但是,如果将x定义为全局显式,则它将使用LOAD_GLOBAL / STORE_GLOBAL。
In the case of print
in your first function, the compiler assumes that since no local variable is declared (assigned) ever in the function body, you should mean a global variable.
在第一个函数中打印的情况下,编译器假定由于函数体中没有声明(赋值)局部变量,因此您应该表示全局变量。
#2
2
The act of assigning to a name, anywhere inside a function, makes that name local only.
在函数内的任何位置分配名称的行为仅使该名称为本地名称。
#3
1
You're using a local
variable , a different binding, not the global one.
您使用的是局部变量,不同的绑定,而不是全局变量。
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. If the definition occurs in a function block, the scope extends to any blocks contained within the defining one, unless a contained block introduces a different binding for the name
范围定义块中名称的可见性。如果在块中定义了局部变量,则其范围包括该块。如果定义发生在功能块中,则范围将扩展到定义块中包含的任何块,除非包含的块为名称引入了不同的绑定
#4
0
Because you have to add global x
to the function:
因为您必须向函数添加全局x:
def fn():
global x
x += 1
print x
This tells the interpreter that you intend to modify a global variable. This is not necessary for certain objects like mutable sequences (e.g. list
).
这告诉解释器您打算修改全局变量。对于像可变序列(例如列表)这样的某些对象,这不是必需的。
#1
4
In Python, assigning to a variable is also an implicit local declaration, that is resolved during the bytecode compilation. So
在Python中,分配给变量也是一个隐式本地声明,在字节码编译期间解析。所以
x += 1
will create a local variable x
and compile to this byte code:
将创建一个局部变量x并编译为此字节代码:
0 LOAD_FAST 0 (x)
3 LOAD_CONST 1 (1)
6 INPLACE_ADD
7 STORE_FAST 0 (x)
The command LOAD_FAST
will try to load a local variable x
which is not yet defined, that's why it fails.
命令LOAD_FAST将尝试加载尚未定义的局部变量x,这就是失败的原因。
However, if you define x
as global
explicitly, then it will use LOAD_GLOBAL
/STORE_GLOBAL
instead.
但是,如果将x定义为全局显式,则它将使用LOAD_GLOBAL / STORE_GLOBAL。
In the case of print
in your first function, the compiler assumes that since no local variable is declared (assigned) ever in the function body, you should mean a global variable.
在第一个函数中打印的情况下,编译器假定由于函数体中没有声明(赋值)局部变量,因此您应该表示全局变量。
#2
2
The act of assigning to a name, anywhere inside a function, makes that name local only.
在函数内的任何位置分配名称的行为仅使该名称为本地名称。
#3
1
You're using a local
variable , a different binding, not the global one.
您使用的是局部变量,不同的绑定,而不是全局变量。
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. If the definition occurs in a function block, the scope extends to any blocks contained within the defining one, unless a contained block introduces a different binding for the name
范围定义块中名称的可见性。如果在块中定义了局部变量,则其范围包括该块。如果定义发生在功能块中,则范围将扩展到定义块中包含的任何块,除非包含的块为名称引入了不同的绑定
#4
0
Because you have to add global x
to the function:
因为您必须向函数添加全局x:
def fn():
global x
x += 1
print x
This tells the interpreter that you intend to modify a global variable. This is not necessary for certain objects like mutable sequences (e.g. list
).
这告诉解释器您打算修改全局变量。对于像可变序列(例如列表)这样的某些对象,这不是必需的。