I'm just curious how python know beforehand that a variable is local and not reference the global variable instead. Please have a look on two versions of the code.
eggs = 'global'
def spam():
print(eggs)
eggs = 'local'
spam()
While, if i comment eggs = 'local', python refer to global variable.
eggs = 'global'
def spam():
print(eggs)
# eggs = 'local'
spam()
Please help me with understanding as to what i'm missing.
Edit: Found the answer here: Python variable scope error