Skip to content

Instantly share code, notes, and snippets.

@dcrosta
Created April 30, 2012 20:17
Show Gist options
  • Save dcrosta/2562350 to your computer and use it in GitHub Desktop.
Save dcrosta/2562350 to your computer and use it in GitHub Desktop.
A Python Mystery
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
Copy link

varun06 commented May 1, 2012 via email

@figgybit
Copy link

figgybit commented May 1, 2012

@dcrosta thanks for the puzzle it was fun and I enjoy your explanation. Tell Mr. Diamond I say 'HI'

Hackers Unite

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment