I'm sure this has been asked and answered, but I couldn't find it specifically:
I'm just picking up Python and I'm not understanding a variable scope issue.
I've simplified the problem to the following:
Case 1:
def lev1():
exec("aaa=123")
print("lev1:",aaa)
lev1()
Case 2:
def lev1():
global aaa
exec("aaa=123")
print("lev1:",aaa)
lev1()
Case 3:
def lev1():
exec("global aaa ; aaa=123")
print("lev1:",aaa)
lev1()
Case 1
andCase 2
haveaaa
undefined in the print statement.Case 3
works. Where doesaaa
actually exist inCase 1
andCase 2
?- Is there a way to access
aaa
in Case 1 without aglobal
declaration?