Global variables (global variable) Variables that are not defined inside any function and have global scope , and local variable (local variable) Are those variables that are defined inside the function and whose scope is limited to the function . let me put it another way , We can say that a local variable can only be accessed inside the function that initializes it , Global variables are accessible throughout the program and within each function .
global keyword : If we want to assign values or change global variables , We just need to use... In the function global keyword . Printing and accessing global variables do not require global keyword . If any variable changed or created inside the function has not been declared as a global variable , Then it is all local variables . Use... Outside the function global Invalid keyword .global Bind global variables .
nonlocal keyword : And global Keyword functions are similar , But for nested functions (nested function) in .nonlocal Bind local variables , It only works for the Bureau , Leave nested functions , Then the variable is invalid .
The above content mainly refers to :https://www.geeksforgeeks.org/global-local-variables-python/
Here is the test code :
var = 6
if var == 1:
# reference: https://www.geeksforgeeks.org/global-local-variables-python/
def f(): # Creating local variables
s = "I love Geeksforgeeks" # local vairable
print("Inside Function:", s)
f()
#print("s:", s) # NameError: name 's' is not defined
elif var == 2:
# reference: https://www.geeksforgeeks.org/global-local-variables-python/
# Defining and accessing global variables
def f(): # This function uses global variable s
print("Inside Function:", s)
# Global scope
s = "I love Geeksforgeeks" # global variable, is used both inside the f function as well as outside the f function
f()
print("Outside Function:", s)
elif var == 3:
# reference: https://www.geeksforgeeks.org/global-local-variables-python/
# This function has a variable with name same as s
def f():
#s += 'GFG' # UnboundLocalError: local variable 's' referenced before assignment
s = "Me too." # If a variable with the same name is also defined in the scope of the function , Then it will only print the values given inside the function , Not global values
print(s)
s = "I love Geeksforgeeks" # global scope
f()
print(s) # I love Geeksforgeeks
elif var == 4:
# reference: https://www.geeksforgeeks.org/global-local-variables-python/
# This function modifies the global variable 's'
def f():
global s # If we want to assign values or change global variables , We just need to use... In the function global keyword
s += ' GFG'
print(s)
s = "Look for Geeksforgeeks Python Section"
print(s)
s = "Python is great!" # global scope
f()
print(s) # Look for Geeksforgeeks Python Section
elif var == 5:
# reference: https://www.geeksforgeeks.org/use-of-nonlocal-vs-use-of-global-keyword-in-python/
def fun():
var1 = 10
def gun():
nonlocal var1 # tell python explicitly that it has to access var1 initialized in fun using the keyword nonlocal
# global var1; var1 = var1 + 10 # NameError: name 'var1' is not defined
var1 = var1 + 10
print(var1) # 20
gun()
print(var1) # 20
fun()
elif var == 6:
# reference: https://www.geeksforgeeks.org/python-nonlocal-keyword/
def geek_func():
geek_name = "geekforgeeks" # local variable to geek_func
def geek_func1(): # First Inner function
geek_name = "GeekforGeeks"
def geek_func2(): # Second Inner function
nonlocal geek_name # Declairing nonlocal variable
geek_name = 'GEEKSFORGEEKS'
print(geek_name) # Printing our nonlocal variable, GEEKSFORGEEKS
geek_func2() # Calling Second inner function
geek_func1() # Calling First inner function
print(geek_name) # Printing local variable to geek_func, geekforgeeks
geek_func()
print("test finish")