Comment

Comment by Erwin

In Python 2, the bytecode optimization that lets you access variables by index is turned off if your function has exec code in it that may modify locals.

So if your function is:

   def foo(): exec "a=1"; return a
Then running dis.dis on foo to disassemble the bytecode it you will see:

              8 LOAD_NAME                0 (a)
while you normally would see:

              6 LOAD_FAST                0 (a)
You can't use the exec in locals thing in Python 3 at all I believe.
parent Erwin 8 years ago

Discussion

Live from the official Hacker News API

A modern reader for Hacker News · SvelteKit + Skeleton