python报错:local variable 'xxxx' referenced before assignment

最近在编码时遇到此错误:local variable ‘xxx’ referenced before assignment,通过查找资料解决了此问题。这篇博客讲的很清楚,故而收藏一下,方便以后查看。


这个问题很囧,在外面定义了一个变量 xxx ,然后在Python的一个函数里面引用这个变量,并改变它的值,结果报错local variable ‘xxx’ referenced before assignment,代码如下:

[python] view plain copy
xxx = 23  
def PrintFileName(strFileName):   
    if xxx == 23:  
        print strFileName  
        xxx = 24  

PrintFileName("file")  

错误的意思就是xxx这个变量在引用前还没有定义,这上面不是定义了么?但是后来我把xxx = 24这句去掉之后,又没问题了,后来想起python中有个global关键字是用来引用全局变量的,尝试了一下,果然可以了:

[python] view plain copy
xxx = 23  
def PrintFileName(strFileName):  
    global xxx  
    if xxx == 23:  
        print strFileName  
        xxx = 24  

PrintFileName("file")  

原来在python的函数中和全局同名的变量,如果你有修改变量的值就会变成局部变量,在修改之前对该变量的引用自然就会出现没定义这样的错误了,如果确定要引用全局变量,并且要对它修改,必须加上global关键字。

原文链接:http://blog.csdn.net/magictong/article/details/4464024


补充:
在类中定义全局变量:

a = 1 
class File(object): 
    def printString(self,str): 
        global a  
        if a == 1:  
            print str  
            a = 24          
f = File()             
f.printString("file")  

猜你喜欢

转载自blog.csdn.net/muwinter/article/details/77324273