問題描述
根據python參考手冊
我們有
當根本找不到名稱時,會引發 NameError 異常.如果該名稱指的是尚未綁定的局部變量,a引發 UnboundLocalError 異常.UnboundLocalError 是一個子類的名稱錯誤.
When a name is not found at all, a NameError exception is raised. If the name refers to a local variable that has not been bound, a UnboundLocalError exception is raised. UnboundLocalError is a subclass of NameError.
我不明白什么時候拋出 UnboundLocalError
?因為
I don't to understand when the UnboundLocalError
is thrown? Because
Python 缺少聲明并允許發生名稱綁定操作代碼塊內的任何位置.
Python lacks declarations and allows name binding operations to occur anywhere within a code block.
那么我們怎樣才能聲明一個變量,而不是初始化她呢?
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
這里 foobar
被引用,但從未被分配到.這會引發 NameError
因為名稱從未綁定.
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
因為 spam = 'eggs'
永遠不會執行,所以 print spam
會引發 UnboudLocalError
.
Because spam = 'eggs'
is never executed, print spam
raises an UnboudLocalError
.
請注意,Python 中沒有任何地方聲明過.您綁定或不綁定,聲明不是語言的一部分.
Note that nowhere in Python is a name ever declared. You bind or don't bind, declaration is not part of the language.
相反,binding 用于確定名稱的范圍;綁定操作包括賦值、用于 for
循環的名稱、函數參數、導入語句、在 except
子句中保存捕獲的異常的名稱、上下文管理器的名稱一個 with
語句所有的綁定名稱.
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.
如果名稱綁定在范圍內(例如在函數中),則它是本地名稱,除非您使用 global
語句(或 nonlocal
語句在 Python 3 中)將名稱顯式標記為全局(或閉包).
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.
所以下面是一個錯誤:
>>> 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
被 somewhere 綁定在 bar
函數范圍內.但是如果您將 foo
標記為全局,則該函數可以工作:
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
因為現在 Python 編譯器知道您希望 foo
改為全局變量.
because now the Python compiler knows you wanted foo
to be a global instead.
所有這些都記錄在 命名和綁定部分 Python 參考文檔.
This is all documented in the Naming and Binding section of the Python reference documentation.
這篇關于未綁定的變量和名稱的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!