Recently I became curious about but what happens in line 2 of the following bogus python code:
def my_fun(foo,bar):
foo
return foo + bar
The reason I became interested is that I'm trying Light Table and tried to put a watch on "foo." It appeared to cause the python interpreter to hang.
Am I correct in thinking that this line has absolutely no effect and does not cause any sort of error? Can someone explain what the interpreter does exactly here?
One can look at what is happening with a little help from the built-in dis module:
The
dis.disfunction disassembles functions (yep, it can disassemble itself), methods, and classes.The output of
dis.dis(my_fun)is:The first two bytecodes are exactly what we need: the
fooline.Here's what these bytecodes do:
fooonto the stack (LOAD_FAST)Basically,
fooline has no effect. (well, iffoovariable is not defined thenLOAD_FASTwill throw theNameError)