简体   繁体   English

当我尝试在 Python3 中的 function 中打印全局变量时出现错误

[英]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(最近一次通话最后一次)在
5 5
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 打印(一)
5 5

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.

相关问题 尝试从类的函数中打印变量时在 python 中出错 - Getting error in python when trying to print a variable from a class' function 从另一个定义函数获取变量以在python3中的另一个定义函数中读取时发生名称错误 - Name Error when getting variable from another define function to be read in another define function in python3 我正在尝试为我正在开发的应用程序制作 GUI,但是当我尝试打印全局变量时出现错误。 为什么? - I am trying to make a GUI for a app I am working on but when I try print a global variable I get an error. Why? 为什么我在 function 中使用局部变量时出现错误,但它是全局变量 - Why am I getting an error that I'm using a local variable inside a function when it is a global variable 当我尝试在函数中使用脚本中的变量时出现错误 - Getting an error when I try to use a variable from my script in my function Python3打印功能 - Python3 Print Function Python3中的print函数 - print function in Python3 “NameError:当我尝试在 Python3 中使用该变量时,未定义名称‘响应’ - "NameError: name 'response' is not defined when i try to use the variable in Python3 为什么我在 function 中的全局值在打印时返回错误? - Why is my global value in a function return an error when i print it? 为什么在尝试用python编程堆栈时将局部变量声明为全局变量时出现局部变量错误? - why am I getting an error of local variable when I have declared it as a global variable, whilst trying to program a stack in python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM