Search code examples
pythonpython-3.xassemblybytecode

more function calls than CALL_FUNCTIONs in dis bytecode


Consider the program:

def wrap():
 print(66)

print(77,77)
wrap()

The dis module (3.5.2) reports that this code means:

  1           0 LOAD_CONST               0 (<code object wrap at 0x7f9d3b538c90, file "<dis>", line 1>)
              3 LOAD_CONST               1 ('wrap')
              6 MAKE_FUNCTION            0
              9 STORE_NAME               0 (wrap)

  4          12 LOAD_NAME                1 (print)
             15 LOAD_CONST               2 (77)
             18 LOAD_CONST               2 (77)
             21 CALL_FUNCTION            2 (2 positional, 0 keyword pair)
             24 POP_TOP

  5          25 LOAD_NAME                0 (wrap)
             28 CALL_FUNCTION            0 (0 positional, 0 keyword pair)
             31 POP_TOP
             32 LOAD_CONST               3 (None)
             35 RETURN_VALUE

This compiled bytecode includes two CALL_FUNCTION operations, but I count three function calls in the code. Why doesn't the print(66) appear?


Solution

  • It's in the code object attached to the function wrap. Try dis(wrap).