-
-
Save dcrosta/2562350 to your computer and use it in GitHub Desktop.
Guess the output: | |
>>> def exec_code_object_and_return_x(codeobj, x): | |
... exec codeobj | |
... return x | |
... | |
>>> co1 = compile("""x = x + 1""", '<string>', 'exec') | |
>>> co2 = compile("""del x""", '<string>', 'exec') | |
>>> exec_code_object_and_return_x(co1, 1) | |
# What do you get here? | |
>>> exec_code_object_and_return_x(co2, 1) | |
# And what about here? | |
@varun06 I've posted my blog entry on this mystery: http://late.am/post/2012/04/30/the-exec-statement-and-a-python-mystery
@figgybit I go into this in a bit more detail in the blog post, but I think the main difference between your first and second examples is that in the first, the exec
'd code object is operating on locals, while in the second there are no locals (in module scope, locals and globals are the same; that is, there are only globals). Thus, in the second case, after exec
ing the code object, the variable x
no longer exists in the (global) scope; in the first example, the variable x
is deleted in the exec
'd code object's frame's locals, but not in the wrapping function's frame's locals.
@dcrosta thanks for the puzzle it was fun and I enjoy your explanation. Tell Mr. Diamond I say 'HI'
Hackers Unite
Thanks @figgybit. As a Python noob, I was assuming that was happening and now I know it was.