【Python】UnboundLocalError: local variable 'l' referenced before assignment
01 在程式中設定的sum_Logical屬於全域性變數,而在函式 calculate中沒有sum_Logical的定義,根據python訪問區域性變數和全域性變數的規則:當搜尋一個變數的時候,python先從區域性作用域開始搜尋,如果在區域性作用域沒有找到那個變數,那樣python就在全域性變數中找這個變數,如果找不到丟擲異常(NAMEERROR 或者 Unbound-LocalError,這取決於python 版本。)
02 如果內部函式有引用外部函式的同名變數或者全域性變數,並且對這個變數有修改.那麼python會認為它是一個區域性變數,又因為函式中沒有sum_Logical的定義和賦值,所以報錯。
03 從下面兩個程式看 單獨的訪問或者修改全域性變數,並不報錯~
04 #!/usr/bin/python
05 import sys
06 sum=5
07 def add(a=1,b=3):
08 print a,b
09 print sum --僅僅訪問
10 add(4,8)
11 print sum
12 [root@rac3 python]# python local.py
13 4 8
14 5
15
16 #!/usr/bin/python
17 import sys
18 sum=5
19 def add(a=1,b=3):
20 print a,b
21 sum=b+a --在函式內部修改
22 print sum
23 add(4,8)
24 [root@rac3 python]# python local.py
25 4 8
26 12
27 下面的程式就會因為“如果內部函式有引用外部函式的同名變數或者全域性變數,並且對這個變數有修改.那麼python會認為它是一個區域性變數,又因為函式中沒有sum_Logical的定義和賦值,所以報錯”
28 #!/usr/bin/python
29 import sys
30 sum=5
31 def add(a=1,b=3):
32 print a,b
33 print sum
34 sum=b+a
35 print sum
36 add(4,8)
37 print sum
38 [root@rac3 python]# python local.py
39 4 8
40 Traceback (most recent call last):
41 File "local.py", line 10, in ?
42 add(4,8)
43 File "local.py", line 6, in add
44 print sum
45 UnboundLocalError: local variable 'sum' referenced before assignment
46 遇到在程式中訪問全域性變數並且要修改全域性變數的值的情況可以使用:global 關鍵字,在函式中宣告此變數是全域性變數
47 #!/usr/bin/python
48 import sys
49 sum=5
50 print '改變之前:sum=',sum
51 def add(a=1,b=3):
52 global sum
53 print 'add 函式中:sum=',sum
54 sum=b+a
55 print '函式中改變之後:sum= ',sum
56 add(4,8)
57 print '改變之後 sum=',sum
58 [root@rac3 python]# vim local.py
59 改變之前:sum= 5
60 add 函式中:sum= 5
61 函式中改變之後:sum= 12
62 改變之後 sum= 12
02 如果內部函式有引用外部函式的同名變數或者全域性變數,並且對這個變數有修改.那麼python會認為它是一個區域性變數,又因為函式中沒有sum_Logical的定義和賦值,所以報錯。
03 從下面兩個程式看 單獨的訪問或者修改全域性變數,並不報錯~
04 #!/usr/bin/python
05 import sys
06 sum=5
07 def add(a=1,b=3):
08 print a,b
09 print sum --僅僅訪問
10 add(4,8)
11 print sum
12 [root@rac3 python]# python local.py
13 4 8
14 5
15
16 #!/usr/bin/python
17 import sys
18 sum=5
19 def add(a=1,b=3):
20 print a,b
21 sum=b+a --在函式內部修改
22 print sum
23 add(4,8)
24 [root@rac3 python]# python local.py
25 4 8
26 12
27 下面的程式就會因為“如果內部函式有引用外部函式的同名變數或者全域性變數,並且對這個變數有修改.那麼python會認為它是一個區域性變數,又因為函式中沒有sum_Logical的定義和賦值,所以報錯”
28 #!/usr/bin/python
29 import sys
30 sum=5
31 def add(a=1,b=3):
32 print a,b
33 print sum
34 sum=b+a
35 print sum
36 add(4,8)
37 print sum
38 [root@rac3 python]# python local.py
39 4 8
40 Traceback (most recent call last):
41 File "local.py", line 10, in ?
42 add(4,8)
43 File "local.py", line 6, in add
44 print sum
45 UnboundLocalError: local variable 'sum' referenced before assignment
46 遇到在程式中訪問全域性變數並且要修改全域性變數的值的情況可以使用:global 關鍵字,在函式中宣告此變數是全域性變數
47 #!/usr/bin/python
48 import sys
49 sum=5
50 print '改變之前:sum=',sum
51 def add(a=1,b=3):
52 global sum
53 print 'add 函式中:sum=',sum
54 sum=b+a
55 print '函式中改變之後:sum= ',sum
56 add(4,8)
57 print '改變之後 sum=',sum
58 [root@rac3 python]# vim local.py
59 改變之前:sum= 5
60 add 函式中:sum= 5
61 函式中改變之後:sum= 12
62 改變之後 sum= 12
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/22664653/viewspace-713234/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- Python的UnboundLocalError: local variable 'xxx' referenced before assignmentPythonError
- Python中的UnboundLocalError: variable referenced before assignment錯誤分析與解決PythonError
- 問題解決local variable 'xxx' referenced before assignment
- global_variable VS local_variable &functionalFunction
- cannot access local variable where it is not associated with a value
- java11新特性--Local-Variable Syntax for Lambda ParametersJava
- dedecms /include/filter.inc.php Local Variable OverridingFilterPHP
- Python UnboundLocalError和NameError錯誤根源解析PythonError
- dedecms /plus/search.php SQL Injection && Local Variable OverridingPHPSQL
- ecshop /pick_out.php SQL Injection Vul By Local Variable OverridingPHPSQL
- thread local in pythonthreadPython
- dedecms /include/uploadsafe.inc.php SQL Injection Via Local Variable Overriding VulPHPSQL
- The entity "nbsp" was referenced, but not declared
- variable: Type 與 Type variable
- Program Units and Referenced Objects (244)Object
- Session State and Referenced Packages (246)SessionPackage
- Algorithm assignment 1Go
- It is indirectly referenced from required .class fileUI
- Oracle environment variableOracle
- CSS Variable StudyCSS
- MySQL 報錯'Variable 'XXX' is a read only variable'MySql
- L1-058 6翻了 pythonPython
- L1-034 點贊 pythonPython
- Assignment Problem的若干思考
- Stock overview and cost assignment - 1View
- Stock overview and cost assignment - 2View
- Stock overview and cost assignment - 3View
- Stock overview and cost assignment - 4View
- Stock overview and cost assignment - 5View
- Stock overview and cost assignment - 6View
- "_OBJC_CLASS_$_XXX", referenced from:OBJ
- jQuery before()jQuery
- ORACLE程式之LOCAL=NO和LOCAL=YESOracle
- 程式碼會引發 Notice: Undefined variable: undefined_variable 錯誤Undefined
- "_dns_free_resource_record", referenced from:問題DNS
- 完美解決stack Error: Can‘t find Python executable “python“, you can set the PYTHON env variable.ErrorPython
- compact (): Undefined variable: operatorUndefined
- L1-075 強迫症 pythonPython