The essential difference between a global variable and a local variable is the scope
To be understood in layman's words,
Global variables are declared throughout the py file and can be accessed at the global scope
A local variable is declared in a function and can be called only in that function, and if you try to call it out of range, the program explodes.
If you define a local variable with the same name as a global variable inside a function, it can cause unexpected effects, which may not be what you expect. It is therefore not recommended to use this, which makes the program very unhealthy
Take a look at a few examples to understand the difference between a global variable and a local variable:
Demo1:
- def fun (x):
- y=2
- Print ("Run result of multiplication:", x*y)
- num1=1
- Print ("Initial num1=", NUM1)
- Fun (NUM1)
- Print ("Y's value is:", y)
Operation Result:
The error is due to an attempt to access a local variable, but the location of the access is not in the scope of the variable y
Demo2:
- def fun ():
- num1=2
- print ("Inside function modified num1=", NUM1)
- num1=1
- Print ("Initial num1=", NUM1)
- Fun ()
- Print ("num1= after running function", NUM1)
Operation Result:
You can see that after modifying the global variable inside the function, the result of the modification is invalid when the function is executed, and the global variable is not affected
Look again:
DEMO3:
- def fun ():
- num1*=2
- print ("Inside function modified num1=", NUM1)
- num1=1
- Print ("Initial num1=", NUM1)
- Fun ()
- Print ("num1= after running function", NUM1)
Operation Result:
An error has been made. This is because the fun () function uses the local variable NUM1, which is just a local variable with the same name as the global variable, and is assigned before use, so again, don't use it.
Global keyword
If you really want to modify the value of a global variable in a function body, use the global keyword
Demo4:
- def fun ():
- Global NUM1
- num1=2
- print ("Inside function modified num1=", NUM1)
- num1=1
- Print ("Initial num1=", NUM1)
- Fun ()
- Print ("num1= after running function", NUM1)
Operation Result:
Using the Global keyword is to tell the Python compiler that the variable is not a local variable but a global variable, which is a bit like the meaning of "reference".
nonlocal keywords
Then look at another keyword associated with the variable nonlocal, which literally means that the current variable is not a local variable. Nonlocal is a new keyword in Python3.0, python2.x does not support
Let's take a look at the following code
- def fun ():
- num2=3
- def fun2 ():
- num2*=2
- print ("num2=", num2)
- return fun2 ()
- Fun ()
Operation Result:
The cause of the error is similar to the previous one, that is, using undefined local variables, but num2 is not a global variable, just the outer variables of the FUN2 function, forcing the use of the global definition of num2 will also be an error (do not believe you try)
This is the time to use the nonlocal keyword:
- def fun ():
- num2=3
- def fun2 ():
- Nonlocal num2
- num2*=2
- print ("num2=", num2)
- return fun2 ()
- Fun ()
Operation Result:
This way, the program will execute normally
The difference between a global variable and a local variable in Python