Variables declared in exec'ed code do not become local in Python 3 - documentation?

The code

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

works on both Python 2 and Python 3, but prints different results. Is this change documented anywhere? (A pointer to a discussion of the mailing list will also be excellent - I ask for this solely out of curiosity.)

+7
source share
2 answers

This is because some hackers have been removed from Python 3.

The new documentation on the exec() function has some notes about this, but does not fully explain the situation.

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

Check out my other answer about this here .

+5
source

Well, there this error report in the error tracker is not really documentation. Nevertheless.

Oh, and this option is even better!

Well, maybe the best .

+1
source

All Articles