On python 3.6,
Code 1:
x="glob"
def reg(x):
print(x)
x="loc"
print(x)
reg(x)
Output:
glob loc
Code 2:
x="glob"
def reg():
print(x)
x="loc"
print(x)
reg()
Output:
Traceback (most recent call last):
File "D:\Projects\Python\abc.py", line 6, in <module>
reg()
File "D:\Projects\Python\abc.py", line 3, in reg
print(x)
UnboundLocalError: local variable 'x' referenced before assignment
Why does this happen? What's the difference if I'm not passing a global variable to a function?? Shouldn't it be accessible to all the functions?