On the few occasions I have had to do this I have used the global keyword. Currently I am cleaning up some code that's not mine which is basically 200 hundred lines after a While statement with no closures. I'd like to separate everything functionally into smaller bits but the problem is that there are a lot of references to the same variables all over those 200 lines. Is there a way to give a nested function access to All Parent Variables without declaring them explicitly?
The original really long while statement:
While True:
var1, var2, var3, var4 = x, [], {}, y
#does something with var2, var3 and var4
#does something with var1 and var3
#does something with var4 and var1
#etc.
to ---> Edit: Would this be an incorrect solution, if so why?
class My:
pass
def foo():
#does something with My.var2, My.var3 and My.var4
def bar():
#does something with My.var1 and My.var3
def tar():
does something with My.var4 and My.var1
While True:
My.var1, My.var2, My.var3, My.var4 = x, [], {}, y
#There are a lot of variables to keep track off and declaring each one
# as global will take a lot of extra declarations
foo()
bar()
tar()