[英]Getting error when I try to print a global variable in a function in Python3
In this simple code to learn differences about global and local variable:在这个简单的代码中学习关于全局变量和局部变量的区别:
def sub():
print(a)
a="banana"
print(a)
a="apple"
sub()
print(a)
I am getting an error:我收到一个错误:
UnboundLocalError
UnboundLocalError
Traceback (most recent call last) in
Traceback(最近一次通话最后一次)在
55
6 a="apple"6 a="苹果"
----> 7 sub()----> 7 个子()
8 print(a)8 打印(一)
in sub()
在子()
1 def sub():1个定义子():
----> 2 print(a)----> 2 打印(一)
3 a="banana"3 a="香蕉"
4 print(a)4 打印(一)
55
UnboundLocalError: local variable 'a' referenced before assignment
UnboundLocalError:分配前引用的局部变量“a”
I am currently understanding that 'a' is a global variable which is declared outside a function.我目前了解'a'是一个全局变量,它在 function之外声明。
(It is not declared on any function like main() in C) (它没有像 C 中的 main() 一样在任何 function 上声明)
But why is this error telling me that 'a' is a local variable?但是为什么这个错误告诉我'a'是一个局部变量?
I know if I add global a
above the print(a)
line will solve this error, but I want to know WHY.我知道如果我在
print(a)
行上方添加global a
将解决这个错误,但我想知道为什么。
Python interprets this line: a="banana"
in the function as the definition of a new, local, variable a
. Python 将这一行: function 中
a="banana"
解释为新的局部变量a
的定义。 This variable in the scope of the function replaces the global variable a
. function 的 scope 中的这个变量替换了全局变量
a
。 Note that print(a)
(reference to local variable a
) occurs before a="banana"
(= assignment).请注意,
print(a)
(对局部变量a
引用)出现在a="banana"
(= 赋值)之前。 Hence you get the error: UnboundLocalError: local variable 'a' referenced before assignment
.因此你得到错误:
UnboundLocalError: local variable 'a' referenced before assignment
。
SEE ALSO:也可以看看:
Why am I getting an UnboundLocalError when the variable has a value? 当变量有值时,为什么我会收到 UnboundLocalError?
Python gotchas Python 陷阱
The 10 Most Common Mistakes That Python Developers Make Python 开发人员最常犯的 10 个错误
The main reason is by placing主要原因是通过放置
print(a)
打印(一)
inside a function sets variable 'a' as a local variable (ie, local scope) of that function.在 function 内部将变量“a”设置为该 function 的局部变量(即局部范围)。 Ignoring the
忽略
a="apple"
一个=“苹果”
defined outside the function.在 function 之外定义。
And as 'a' value has not been initialized inside the 'sub' function hence its value couldn't be found while executing print(a) hence shows local variable 'a' referenced before assignment which is exactly what happens in the above case.由于 'a' 值尚未在 'sub' function 内部初始化,因此在执行 print(a) 时找不到它的值,因此显示在赋值之前引用的局部变量 'a' 这正是在上述情况下发生的情况。
FOR SUMMARY总结
def sub():
# print(a) #Just comment this and you will understand the difference
## By doing print(a) inside sub function makes sets 'a' as local variable
## whose value has not been initialized
## and as its value couldn't be found while line print(a) executes hence shows
## local variable 'a' referenced before assignment which is exactly what
## happens
a="banana" # a is assigned here
print(a)
a="apple"
sub()
print(a)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.