Python的UnboundLocalError: local variable 'xxx' referenced before assignment
http://blog.csdn.net/onlyanyz/article/details/45009697
結果執行時就出現了上述錯誤。
因為函式內部並沒有再次對n賦值,所以這時,print n列印的就是全域性變數n的值。
結果如下:
UnboundLocalError: local variable 'xxx' referenced before assignment
如下程式碼片所示:
- n=0
- def func():
- print n
- n+=1
- func()
結果執行時就出現了上述錯誤。
這是因為在函式內部對變數賦值進行修改後,該變數就會被直譯器認為是區域性變數而非全域性變數,當程式執行到n+=1的時候,因為這條語句是給n賦值,所以n成為了區域性變數,那麼在執行print n的時候,因為n這個區域性變數還沒有定義,自然就會丟擲這樣的錯誤。
考慮如下程式碼片:
- n=0
- def func():
- print n
- func()
那麼,我們怎樣才能達到在函式內部先列印,再賦值的操作呢?結論就是使用global關鍵字,在函式內部先宣告n這個變數是全域性變數。程式碼如下:
- n=0
- def func():
- global n
- print n
- n+=1
- func()
- print n
- 0
- 1
- [Finished in 1.0s]
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/310974/viewspace-2142809/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 【Python】UnboundLocalError: local variable 'l' referenced before assignmentPythonError
- 問題解決local variable 'xxx' referenced before assignment
- Python中的UnboundLocalError: variable referenced before assignment錯誤分析與解決PythonError
- global_variable VS local_variable &functionalFunction
- "_OBJC_CLASS_$_XXX", referenced from:OBJ
- MySQL 報錯'Variable 'XXX' is a read only variable'MySql
- cannot access local variable where it is not associated with a value
- MySQL 中出現報錯提示: ‘Variable ‘XXX‘ is a read only variable‘的解決方法MySql
- The type XXX cannot be resolved.It is indirectly referenced from required .classUI
- java11新特性--Local-Variable Syntax for Lambda ParametersJava
- dedecms /include/filter.inc.php Local Variable OverridingFilterPHP
- Python UnboundLocalError和NameError錯誤根源解析PythonError
- 開發中遇到的bug-Property or method “xxxx“ is not defined on the instance but referenced during render.
- dedecms /plus/search.php SQL Injection && Local Variable OverridingPHPSQL
- ecshop /pick_out.php SQL Injection Vul By Local Variable OverridingPHPSQL
- Mysql:Variable 'XXXt' can't be set to the value of 'NULL'解決MySqlNull
- thread local in pythonthreadPython
- EXT2-fs (dm-1): error: ext2_lookup deleted inode referenced 23422324XXXXErrordelete
- dedecms /include/uploadsafe.inc.php SQL Injection Via Local Variable Overriding VulPHPSQL
- The entity "nbsp" was referenced, but not declared
- variable: Type 與 Type variable
- Assignment Problem的若干思考
- Program Units and Referenced Objects (244)Object
- Session State and Referenced Packages (246)SessionPackage
- Algorithm assignment 1Go
- ORACLE之手動註冊監聽listener。alter system set local_listener="XXX"Oracle
- It is indirectly referenced from required .class fileUI
- xxx,一個神奇的 Python 庫Python
- Error querying database. XXXXXXXXXXXXX, No database selected。ErrorDatabase
- Error building Player: Win32Exception: ApplicationName=‘xxxxxxxxxxxxxxxxxx//sdk\tools\zipalign.exe' ...ErrorUIWin32ExceptionAPP
- Internal Error with background job scheduling XXXXXXXXXXXXX.Error
- Before的翻譯
- // @require file://C:xxxxxxxxx.jsUIJS
- Oracle environment variableOracle
- CSS Variable StudyCSS
- 【Python】pyenv的版本控制及global、shell、local命令區別Python
- LOCAL_SHARED_LIBRARIES 與 LOCAL_LDLIBS,LOCAL_LDFLAGS的區別
- Stock overview and cost assignment - 1View