Python Forum

Full Version: Variable scope issue
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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 !
No. Variables on the left hand side of the = operator are local by default.
Thank you !