12

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 and Case 2 have aaa undefined in the print statement.
  • Case 3 works. Where does aaa actually exist in Case 1 and Case 2?
  • Is there a way to access aaa in Case 1 without a global declaration?
Hara
  • 1,467
  • 4
  • 18
  • 35
user8425543
  • 129
  • 1
  • 3

1 Answers1

17

From the docs:

Note: The default locals act as described for function locals() below: modifications to the default locals dictionary should not be attempted. Pass an explicit locals dictionary if you need to see effects of the code on locals after function exec() returns.

In other words, if you call exec with one argument, you're not supposed to try to assign any variables, and Python doesn't promise what will happen if you try.

You can have the executed code assign to globals by passing globals() explicitly. (With an explicit globals dict and no explicit locals dict, exec will use the same dict for both globals and locals.)

def lev1():
   exec("aaa=123", globals())
   print("lev1:", aaa)

lev1()
user2357112
  • 260,549
  • 28
  • 431
  • 505
  • the problem is that you have actually modified 'aaa' in the global scope and not in the scope of the function. To verify, put 'aaa = None' on the first line of the lev1() – ikamen Sep 11 '19 at 21:19
  • @ikamen: Well, yeah. I was pretty explicit about this assigning to globals. You can't assign to a function's locals with `exec`. – user2357112 Sep 12 '19 at 00:32