Python global variables
and the global keyword
in Python variable usage, this error is often encountered :
local variable 'a' referenced before assignment
means that the local variable “a” is referenced before assignment.
such as running the following code will cause this problem:
a = 3
def Fuc():
print (a)
a = a + 1
Fuc()
, but if you delete a = a + 1, the above problem will not occur.
a = 3
def Fuc():
print (a)
Fuc()
it turns out that in Python, a = 3 defines the global variable a, scope to the end of the code from the definition, in a = 3 of the following functions can be cited the global variable a, but if you want to modify the functions and global variables with the same name, the function of the variable will become a local variable, before the change of the variable reference nature presents undistributed or undefined error.
if you are sure to reference and modify global variables you must add the global keyword
a = 3
def Fuc():
global a
print (a)
a=a+1
Fuc()
note: which function needs to modify the global variable, just declare it in the function.
but there is a special function, and that is the main function:
a = 3
def Fuc():
global a
print (a) # 1
a = a + 1
if __name__ == "__main__":
print (a) # 2
a = a + 1
Fuc()
print (a) # 3
output is as follows (in Python3) :
3
4
5
three prints are executed in the order of 2, 1, 3. You can see that there is no global declared variable A in the main function, but you can still modify the global variable A. In a normal function, the global variable A needs to be declared globally in order to modify it.
life is short, I use Python~
Read More:
- Error: global variable is ambiguous (conflict between using namespace STD and global variable)
- How to read JS file app.ux Global variables in
- Error c2951: template declaration can only be used at the global, namespace, or class scope. Error c2598: link specification must be at the global scope
- Vue element admin configure global styles
- Global variable error: unboundlocalerror: local variable ‘l’ referenced before assignment
- Maven global configuration
- Node configuration environment variable and global installation of webapck
- JNI ERROR global reference table overflow [How to Solve]
- AWS appasync error global.TYPE.xx
- ASP.NET Parser Error Message: Could not load type ‘Web.Global’.
- Resolution of problems with rviz global status displayed as error
- Rviz global status is displayed as the problem resolution of error
- [error] OpenEvent(“Global\ngx_reload_11812“) failed (2: The system cannot find the file specified
- Using global timing task cronutil in hutool tool class
- Quickly solve the problem of Vmware virtual machine “\\.\Global\vmx86”: the system cannot find the specified file
- Error in the latest version of ffmpeg: ‘codec’_ FLAG_ GLOBAL_ Header ‘undeclared identifier solution
- Register global time filter in main.js in Vue
- [unity problem] what should I do if I encounter ‘Global::’ already contains a definition
- Jenkins appears error: error cloning remote repo ‘origin solution (set global tool configuration)
- Global lock, table lock and row lock in MySQL