So there is a global scope everything which is outside of a function and there also local scope which is inside of a function.
1
2
3
4
5
6
7
8
9
10
11
# they are threating each other as the different namespacestemp='global'defchange():temp='local'print(temp)# 'local'print(temp)# 'global'defchange():globaltemptemp='local'print(temp)# local
If we want to overwrite this behaviour we should use global operator
The same behaviour we would see if we try to change a variable which is defined inside of a function.
1
2
3
4
5
6
7
defmain():temp='main local'defwrapper():temp='wrapper local'print("Inside of a wrapper",temp)wrapper()# Inside of a wrapper local tempprint("Inside of main",temp)# Inside of main value
But if we want to access a variable, which is instantiated inside of a local scope (inside of a function) we need to use a local operator
1
2
3
4
5
6
7
defmain():temp='main local'defwrapper():localtemp# We used a local operator,# now we can impact the variable defined inside of main temp='wrapper local'