by Erwin · 8y ago · 0 comments
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. def foo():
exec("a=1")
return a
print(foo())
Fails with a NameError: Traceback (most recent call last):
File "test.py", line 5, in <module>
print(foo())
File "test.py", line 3, in foo
return a
NameError: name 'a' is not defined exec("a = 1")
print(a)
This will print "1".But you can do this:
def foo():
exec("a = 1")
return locals()['a']