If this code is all inside the function, but global will not work, because then x will not be a global variable. In Python 3.x, they introduced the nonlocal keyword, which will make the code work regardless of whether it is at the top level or inside a function:
x=True def stupid(): nonlocal x x=False stupid() print x
newacct
source share