Python Forum
Variable scope issue - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Variable scope issue (/thread-35670.html)



Variable scope issue - melvin13 - Nov-29-2021

Hello,
here's a simplified version of what's causing me trouble :
x = int

def init(): # 
    x = 0
    return

def f():
    x += 1
    return

init()
f()
f()
print(x)
Error:
8: Local variable 'x' defined in enclosing scope on line 1 referenced before assignement
What does this error mean in this context ? Aren't variables defined outside of functions set to global by default ?
Thank you !


RE: Variable scope issue - Gribouillis - Nov-29-2021

No. Variables on the left hand side of the = operator are local by default.


RE: Variable scope issue - melvin13 - Nov-29-2021

Thank you !