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:
- Python global variables and global keywords
- Global variable error: unboundlocalerror: local variable ‘l’ referenced before assignment
- Solving Python error: local variable ‘a’ referenced before assignment
- Error: global variable is ambiguous (conflict between using namespace STD and global variable)
- Idapython: the solution of importing “site” failed error
- Can’t find Python executable “D:\python3\python.exe”, you can set the PYTHON env variable.
- Mounriver studio error handling
- From in Python__ future__ The role of import *
- Node configuration environment variable and global installation of webapck
- Go start error: Panic: runtime error: invalid memory address or nil pointer reference
- Exponentiation in shell
- Vue element admin configure global styles
- Mac brew fails to work and an error is reported
- Python: print syntax error: problem with invalid syntax error
- Python 3 install pyqt5
- Solve the problem of Python in Windows environment: Fatal error in launcher: Unable to create process using’”‘ in pip installation
- Differences between using VaR and omitting VaR to define variables in JS
- After upgrading php7, PHP program prompts an error: operator not supported for strings in causes and Solutions
- In Python, print() prints to remove line breaks
- MySQL 8 — error in setting simple password error 1819 (HY000): your password does not satisfy the current policy requirements