19

In Python 2.6,

>>> exec "print (lambda: a)()" in dict(a=2), {}
2
>>> exec "print (lambda: a)()" in globals(), {'a': 2}
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<string>", line 1, in <module>
  File "<string>", line 1, in <lambda>
NameError: global name 'a' is not defined
>>> exec "print (lambda: a).__closure__" in globals(), {'a': 2}
None

I expected it to print 2 twice, and then print a tuple with a single cell. It is the same situation in 3.1. What's going on?

Devin Jeanpierre
  • 92,913
  • 4
  • 55
  • 79

1 Answers1

27

When you pass a string to exec or eval, it compiles that string to a code object before considering globals or locals. So when you say:

eval('lambda: a', ...)

it means:

eval(compile('lambda: a', '<stdin>', 'eval'), ...)

There's no way for compile to know that a is a freevar, so it compiles it to a global reference:

>>> c= compile('lambda: a', '<stdin>', 'eval')
>>> c.co_consts[0]
<code object <lambda> at 0x7f36577330a8, file "<stdin>", line 1>
>>> dis.dis(c.co_consts[0])
  1           0 LOAD_GLOBAL              0 (a)
              3 RETURN_VALUE        

Therefore to make it work you have to put a in the globals and not the locals.

Yeah, it's a bit dodgy. But then that's exec and eval for you I suppose... they're not supposed to be nice.

bobince
  • 528,062
  • 107
  • 651
  • 834
  • +1. I've been refreshing this thread once every minute since it was posted to find out the answer to this one. Thanks to you I am now free to leave my puter for a while and go out and enjoy the sun. Thank you! ;) – Mia Clarke May 01 '10 at 12:04
  • To make closures work with exec, I provide a trick in [this answer](https://stackoverflow.com/a/72340953/9758790). In short, wrap the original codes that define a function with a temp helper fucntion, so that the `compile` can recognize freevar like `a`. – hellohawaii May 26 '22 at 05:41
  • Is the new `closure` parameter to `exec()` in 3.11 related to solving this? See also https://stackoverflow.com/questions/75941073/give-an-example-explanation-of-the-closure-parameter-of-the-exec-function – Barmar Apr 05 '23 at 15:12