A note about Python scope
Create a variable val with the same name as a global variable and a local variable and check the movement
val = 100 #Global variables
def showVal():
val = 1 #Local variables
print("local val : ",val)
showVal()
print("global val : ",val)
Execution result
local val : 1
global val : 100
Assign a value to a global variable in a function
val = 100 #Global variables
def showVal():
global val #Clarify that variables are global
val = 1
print("local val : ",val)
showVal()
print("global val : ",val)
Execution result
local val : 1
global val : 1
Recommended Posts