Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

`LOAD_FAST` in dis module

Tags:

python

In 32.12. dis — Disassembler for Python bytecode — Python 3.6.3 documentation, there's an example saying:

Example: Given the function myfunc():

def myfunc(alist):
    return len(alist)

the following command can be used to display the disassembly of myfunc():

>>> dis.dis(myfunc)
  2           0 LOAD_GLOBAL              0 (len)
              2 LOAD_FAST                0 (alist)
              4 CALL_FUNCTION            1
              6 RETURN_VALUE

I can understand LOAD_GLOBAL CALL_FUNCTION and RETURN_VALUE

What's the meaning of LOAD_FAST


1 Answers

According to the Python dis docs:

LOAD_FAST(var_num)

Pushes a reference to the local co_varnames[var_num] onto the stack.

So, in your case, the LOAD_FAST instruction loads alist to be able to pass it to the global len function:

return        len          (alist)

RETURN_VALUE  LOAD_GLOBAL  LOAD_FAST
                < CALL_FUNCTION >
like image 171
Daniel Avatar answered Jan 21 '26 08:01

Daniel