I have a function which will recursively execute another function inside and I want to share variable for all execution of that function.
Something like that:
def testglobal():
x = 0
def incx():
global x
x += 2
incx()
return x
testglobal() # should return 2
However, I'm getting error NameError: name 'x' is not defined
There is hacky solution to make list and use first value of that list as x. But this is so ugly.
So how can I share x with incx function ? Or should I use completely different approach ?
xwithout alterations, but assigning (e.g.x = 1) insideincxwill makexlocal toincxand therefore not refer to the same variable.nonlocalachieves this.