Recently, this error was encountered in coding: local variable ‘XXX’ Referenced before assignment, which solved this problem by looking up materials. This blog is very clear, so I collect it for you to view later.
Referenced Before Assignment Local Variable ‘XXX’ Referenced Before Assignment Local Variable ‘XXX’ Referenced Before Assignment Local Variable ‘XXX’ Referenced Before Assignment
[python] view plain copy
xxx = 23
def PrintFileName(strFileName):
if xxx == 23:
print strFileName
xxx = 24
PrintFileName("file")
Error means that the variable ‘XXX’ is not defined before the reference. The global keyword in Python is used to refer to global variables, so I tried it, and it worked: * * * * * * * * * * * * * * * * * * * *
[python] view plain copy
xxx = 23
def PrintFileName(strFileName):
global xxx
if xxx == 23:
print strFileName
xxx = 24
PrintFileName("file")
In Python, if you change the value of a variable with the same global name, it will become a local variable. The reference to that variable will be undefined before you change it. If you want to reference a global variable, and if you want to change it, you must include the global keyword.
The original link: http://blog.csdn.net/magictong/article/details/4464024
Added:
defines global variables in the class:
a = 1
class File(object):
def printString(self,str):
global a
if a == 1:
print str
a = 24
f = File()
f.printString("file")
Read More:
- Solving Python error: local variable ‘a’ referenced before assignment
- Python UnboundLocalError: local variable ‘num’ referenced before assignment
- Global variable error: unboundlocalerror: local variable ‘l’ referenced before assignment
- Differences between shell script variable assignment and C language variable assignment
- Can’t find Python executable “D:\python3\python.exe”, you can set the PYTHON env variable.
- non-static variable this cannot be referenced from a static context
- Python Numpy.ndarray ValueError:assignment destination is read-only
- Error: global variable is ambiguous (conflict between using namespace STD and global variable)
- Python error type error: ‘range’ object does not support item assignment, solution
- Error in installing Matplotlib Library: permissionerror: [errno 13] permission denied: ‘/ usr / local / lib / python3.6’
- Cannot remove entries from nonexistent file /usr/local/bin/anaconda2/lib/python2.7/site-packages/eas
- Error lnk1120: 2 unresolved external commands: vtkrenderingopengl_ Autoinit (construct, destroy), which is referenced in
- The spring boot project directly replaces the referenced jar package
- Using the decorator may prompt property assignment expected. Vetur (1136)
- Arrow function should not return assignment no-return-assign
- Assignment under multiple single edges is not supported for synthesis
- Python global variables and global keywords
- Node configuration environment variable and global installation of webapck
- MySQL error code 1217 (ER_ROW_IS_REFERENCED): Cannot delete or update a parent row: a foreign key co
- C + + programming fault handling — error: assignment of read only data member ‘STD:: pair