问题描述
遵循我们拥有的python reference manual
我不知道UnboundLocalError
是什么时候引起的?因为
那么我们如何声明一个变量,而不初始化它?
您可以引用而不需要为其分配名称:
>>> foobar
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'foobar' is not defined
此处引用了foobar
,但从未分配.这引起了NameError
,因为该名称从未绑定.
更巧妙的是,这里的分配不会发生,因为行永远不会运行:
>>> def foo():
... if False:
... spam = 'eggs'
... print spam
...
>>> foo()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 4, in foo
UnboundLocalError: local variable 'spam' referenced before assignment
由于从未执行过spam = 'eggs'
,因此print spam
会引发UnboudLocalError
.
请注意,声明在Python中从来没有一个名字.绑定或不绑定,声明不是语言的一部分.
相反, binding 用于确定名称的范围;绑定操作包括赋值,用于for
循环的名称,函数参数,import语句,在except
子句中保存捕获的异常的名称,在with
语句中的上下文管理器的名称所有绑定名称. /p>
如果名称在范围内(例如在函数中)绑定,则它是本地名称,除非您使用global
语句(或Python 3中的nonlocal
语句)将名称显式标记为而不是全局(或闭包).
以下是错误:
>>> foo = None
>>> def bar():
... if False:
... foo = 'spam'
... print foo
...
>>> bar()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 4, in bar
UnboundLocalError: local variable 'foo' referenced before assignment
因为foo
在bar
功能范围内某处被绑定.但是,如果将foo
标记为全局变量,则该函数将起作用:
>>> foo = None
>>> def bar():
... global foo
... if False:
... foo = 'spam'
... print foo
...
>>> bar()
None
因为现在Python编译器知道您希望foo
成为全局变量.
所有内容都记录在命名和绑定部分.
Follows to python reference manual
we have
I don't to undertand when does UnboundLocalError
caused? Because
So how we can declare a variable, but not to initialize her?
You can refer to a name without having assigned to it:
>>> foobar
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'foobar' is not defined
Here foobar
is being referred to, but was never assigned to. This raises a NameError
because the name was never bound.
More subtly, here assignment is not happening because the line that does is never run:
>>> def foo():
... if False:
... spam = 'eggs'
... print spam
...
>>> foo()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 4, in foo
UnboundLocalError: local variable 'spam' referenced before assignment
Because spam = 'eggs'
is never executed, print spam
raises an UnboudLocalError
.
Note that nowhere in Python is a name ever declared. You bind or don't bind, declaration is not part of the language.
Instead, binding is used to determine the scope of a name; binding operations include assignment, names used for a for
loop, function parameters, import statements, name to hold a caught exception in an except
clause, the name for a context manager in a with
statement all bind names.
If a name is bound in a scope (such as in a function) then it is a local name, unless you use a global
statement (or a nonlocal
statement in Python 3) to explicitly mark the name as a global (or a closure) instead.
So the following is an error:
>>> foo = None
>>> def bar():
... if False:
... foo = 'spam'
... print foo
...
>>> bar()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 4, in bar
UnboundLocalError: local variable 'foo' referenced before assignment
because foo
is being bound somewhere in the bar
function scope. But if you mark foo
as a global, the function works:
>>> foo = None
>>> def bar():
... global foo
... if False:
... foo = 'spam'
... print foo
...
>>> bar()
None
because now the Python compiler knows you wanted foo
to be a global instead.
This is all documented in the Naming and Binding section of the Python reference documentation.
这篇关于未绑定变量和名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!