7

The code

x = 3
def f():
    exec("x = 2")
    print(x)
f()

runs on both Python 2 and Python 3, but prints different results. Is this change documented anywhere? (A pointer to a mailing list discussion would also be fine -- I ask this purely out of curiosity.)

Sven Marnach
  • 574,206
  • 118
  • 941
  • 841

2 Answers2

5

That's because some hackery were removed from Python 3.

The new documentation about the exec() function has some Notes about that but don't fully explain the situtation.

Python 2, after seeing a exec statement, change every access to vars and functions to LOAD_NAME instead of LOAD_FAST or LOAD_GLOBAL.

Check my other answer about that here.

Community
  • 1
  • 1
JBernardo
  • 32,262
  • 10
  • 90
  • 115
1

Well, there's this bug report in the bug tracker -- not exactly documentation, though.

Ah, and this one is even better!

Okay, possibly the best one yet.

Ethan Furman
  • 63,992
  • 20
  • 159
  • 237