6 ms·
And this: >>> dis.dis(exec_code_object_and_return_x) 2 0 LOAD FAST 0 (codeobj) 3 LOAD_CONST 0 (None)
by iclelland 14y ago
And this:
>>> dis.dis(exec_code_object_and_return_x)
2 0 LOAD FAST 0 (codeobj)
3 LOAD_CONST 0 (None)
6 DUP_TOP
7 EXEC_STMT
3 8 LOAD_FAST 1 (x)
11 RETURN_VALUE
The interesting case is the second one, in which the compiled codeobj uses DELETE_NAME, which doesn't throw an error (if "del x" was in a function, it would use DELETE_FAST, which would throw an UnboundLocalError if it was used before x was assigned,) and the function then uses LOAD_FAST, which references the function argument directly, and so retrieves the value for return.
By compiling and injecting bytecode like this, the program ends up using two different methods of variable access, which seem to sidestep each other completely.