Bytecode em Python


Para visualizar o bytecode de uma função f(), digite:
import dis
def f():
  return 1,
dis.dis(f)

Neste exemplo, o resultado será:
  2           0 LOAD_CONST               2 ((1,))
              3 RETURN_VALUE

Para se conseguir um formato mais legível, uma opção é utilizar o programa de disassemble disponível em Exploring python bytecode.

Uma maior explicação sobre cada bytecode pode ser encontrada em Docs Python: Dis.

Os principais comandos abaixo foram embutidos em funções para serem mostrados em formato de bytecode:

#---------- imprimaId(x)
def imprimaId(x):
    print x
 
OFFSET INSTRUCTION          OPCODE    ARG  TYPE                  VALUE
    0  LOAD_FAST            (124)       0  LOCAL VARIABLE        (x)
    3  PRINT_ITEM           ( 71)
    4  PRINT_NEWLINE        ( 72)
    5  LOAD_CONST           (100)       0  CONSTANT              (None)
    8  RETURN_VALUE         ( 83)
 
#---------- imprimaCon()
def imprimaCon():
    print 123.45
 
OFFSET INSTRUCTION          OPCODE    ARG  TYPE                  VALUE
    0  LOAD_CONST           (100)       1  CONSTANT              (123.45)
    3  PRINT_ITEM           ( 71)
    4  PRINT_NEWLINE        ( 72)
    5  LOAD_CONST           (100)       0  CONSTANT              (None)
    8  RETURN_VALUE         ( 83)
 
#---------- se(x)
def se(x):
    i=1
    if(x==1):
        print x,
    i=2
 
OFFSET INSTRUCTION          OPCODE    ARG  TYPE                  VALUE
    0  LOAD_CONST           (100)       1  CONSTANT              (1)
    3  STORE_FAST           (125)       1  LOCAL VARIABLE        (i)
    6  LOAD_FAST            (124)       0  LOCAL VARIABLE        (x)
    9  LOAD_CONST           (100)       1  CONSTANT              (1)
   12  COMPARE_OP           (106)       2  COMPARE OPERATOR      (==)
   15  JUMP_IF_FALSE        (111)       8  RELATIVE JUMP         (26)
   18  POP_TOP              (  1)
   19  LOAD_FAST            (124)       0  LOCAL VARIABLE        (x)
   22  PRINT_ITEM           ( 71)
   23  JUMP_FORWARD         (110)       1  RELATIVE JUMP         (27)
   26  POP_TOP              (  1)
   27  LOAD_CONST           (100)       2  CONSTANT              (2)
   30  STORE_FAST           (125)       1  LOCAL VARIABLE        (i)
   33  LOAD_CONST           (100)       0  CONSTANT              (None)
   36  RETURN_VALUE         ( 83)
 
#---------- se_senao(x)
def se_senao(x):
    if(x==1):
        print x,
    else:
        print 123.45,
 
OFFSET INSTRUCTION          OPCODE    ARG  TYPE                  VALUE
    0  LOAD_FAST            (124)       0  LOCAL VARIABLE        (x)
    3  LOAD_CONST           (100)       1  CONSTANT              (1)
    6  COMPARE_OP           (106)       2  COMPARE OPERATOR      (==)
    9  JUMP_IF_FALSE        (111)       8  RELATIVE JUMP         (20)
   12  POP_TOP              (  1)
   13  LOAD_FAST            (124)       0  LOCAL VARIABLE        (x)
   16  PRINT_ITEM           ( 71)
   17  JUMP_FORWARD         (110)       5  RELATIVE JUMP         (25)
   20  POP_TOP              (  1)
   21  LOAD_CONST           (100)       2  CONSTANT              (123.45)
   24  PRINT_ITEM           ( 71)
   25  LOAD_CONST           (100)       0  CONSTANT              (None)
   28  RETURN_VALUE         ( 83)
 
#---------- atribuicaoId(x)
def atribuicaoId(x):
    y=x
 
OFFSET INSTRUCTION          OPCODE    ARG  TYPE                  VALUE
    0  LOAD_FAST            (124)       0  LOCAL VARIABLE        (x)
    3  STORE_FAST           (125)       1  LOCAL VARIABLE        (y)
    6  LOAD_CONST           (100)       0  CONSTANT              (None)
    9  RETURN_VALUE         ( 83)
 
#---------- atribuicaoCon()
def atribuicaoCon():
    y=123.45
 
OFFSET INSTRUCTION          OPCODE    ARG  TYPE                  VALUE
    0  LOAD_CONST           (100)       1  CONSTANT              (123.45)
    3  STORE_FAST           (125)       0  LOCAL VARIABLE        (y)
    6  LOAD_CONST           (100)       0  CONSTANT              (None)
    9  RETURN_VALUE         ( 83)
 
#---------- atribuicaoCadeia()
def atribuicaoCadeia():
    s="one"
 
OFFSET INSTRUCTION          OPCODE    ARG  TYPE                  VALUE
    0  LOAD_CONST           (100)       1  CONSTANT              ('one')
    3  STORE_FAST           (125)       0  LOCAL VARIABLE        (s)
    6  LOAD_CONST           (100)       0  CONSTANT              (None)
    9  RETURN_VALUE         ( 83)
 
#---------- nop()
def nop():
    pass
 
OFFSET INSTRUCTION          OPCODE    ARG  TYPE                  VALUE
    0  LOAD_CONST           (100)       0  CONSTANT              (None)
    3  RETURN_VALUE         ( 83)
 
#---------- bloco()
def bloco():
    if(x!=2):
        print x,
        x=x+1;
    else:
        print 'nao',
        print 123.45,
 
OFFSET INSTRUCTION          OPCODE    ARG  TYPE                  VALUE
    0  LOAD_FAST            (124)       0  LOCAL VARIABLE        (x)
    3  LOAD_CONST           (100)       1  CONSTANT              (2)
    6  COMPARE_OP           (106)       3  COMPARE OPERATOR      (!=)
    9  JUMP_IF_FALSE        (111)      18  RELATIVE JUMP         (30)
   12  POP_TOP              (  1)
   13  LOAD_FAST            (124)       0  LOCAL VARIABLE        (x)
   16  PRINT_ITEM           ( 71)
   17  LOAD_FAST            (124)       0  LOCAL VARIABLE        (x)
   20  LOAD_CONST           (100)       2  CONSTANT              (1)
   23  BINARY_ADD           ( 23)
   24  STORE_FAST           (125)       0  LOCAL VARIABLE        (x)
   27  JUMP_FORWARD         (110)       9  RELATIVE JUMP         (39)
   30  POP_TOP              (  1)
   31  LOAD_CONST           (100)       3  CONSTANT              ('nao')
   34  PRINT_ITEM           ( 71)
   35  LOAD_CONST           (100)       4  CONSTANT              (123.45)
   38  PRINT_ITEM           ( 71)
   39  LOAD_CONST           (100)       0  CONSTANT              (None)
   42  RETURN_VALUE         ( 83)
 
#---------- expr(x)
def expr(x):
  r=-x**2+1
  print r,
  return r
 
OFFSET INSTRUCTION          OPCODE    ARG  TYPE                  VALUE
    0  LOAD_FAST            (124)       0  LOCAL VARIABLE        (x)
    3  LOAD_CONST           (100)       1  CONSTANT              (2)
    6  BINARY_POWER         ( 19)
    7  UNARY_NEGATIVE       ( 11)
    8  LOAD_CONST           (100)       2  CONSTANT              (1)
   11  BINARY_ADD           ( 23)
   12  STORE_FAST           (125)       1  LOCAL VARIABLE        (r)
   15  LOAD_FAST            (124)       1  LOCAL VARIABLE        (r)
   18  PRINT_ITEM           ( 71)
   19  LOAD_FAST            (124)       1  LOCAL VARIABLE        (r)
   22  RETURN_VALUE         ( 83)
 
#---------- enquanto(x)
def enquanto(x):
    while(x<10):
        x=x+1
        print x,
 
OFFSET INSTRUCTION          OPCODE    ARG  TYPE                  VALUE
    0  SETUP_LOOP           (120)      32  RELATIVE JUMP         (35)
    3  LOAD_FAST            (124)       0  LOCAL VARIABLE        (x)
    6  LOAD_CONST           (100)       1  CONSTANT              (10)
    9  COMPARE_OP           (106)       0  COMPARE OPERATOR      (<)
   12  JUMP_IF_FALSE        (111)      18  RELATIVE JUMP         (33)
   15  POP_TOP              (  1)
   16  LOAD_FAST            (124)       0  LOCAL VARIABLE        (x)
   19  LOAD_CONST           (100)       2  CONSTANT              (1)
   22  BINARY_ADD           ( 23)
   23  STORE_FAST           (125)       0  LOCAL VARIABLE        (x)
   26  LOAD_FAST            (124)       0  LOCAL VARIABLE        (x)
   29  PRINT_ITEM           ( 71)
   30  JUMP_ABSOLUTE        (113)       3  OTHER                 (3)
   33  POP_TOP              (  1)
   34  POP_BLOCK            ( 87)
   35  LOAD_CONST           (100)       0  CONSTANT              (None)
   38  RETURN_VALUE         ( 83)
 
#---------- funcao_que_chama(w,z)
def funcao_que_chama(w,z):
    return funcao_chamada(w,z)
 
OFFSET INSTRUCTION          OPCODE    ARG  TYPE                  VALUE
    0  LOAD_GLOBAL          (116)       0  GLOBAL VARIABLE       (funcao_chamada)
    3  LOAD_FAST            (124)       0  LOCAL VARIABLE        (w)
    6  LOAD_FAST            (124)       1  LOCAL VARIABLE        (z)
    9  CALL_FUNCTION        (131)       2  OTHER                 (2)
   12  RETURN_VALUE         ( 83)
 
#---------- funcao_chamada(w,z)
def funcao_chamada(w,z):
    return (w*z)
 
OFFSET INSTRUCTION          OPCODE    ARG  TYPE                  VALUE
    0  LOAD_FAST            (124)       0  LOCAL VARIABLE        (w)
    3  LOAD_FAST            (124)       1  LOCAL VARIABLE        (z)
    6  BINARY_MULTIPLY      ( 20)
    7  RETURN_VALUE         ( 83)
 
#---------- leia()
def leia():
    x=input('msg')
    return x
 
OFFSET INSTRUCTION          OPCODE    ARG  TYPE                  VALUE
    0  LOAD_GLOBAL          (116)       0  GLOBAL VARIABLE       (input)
    3  LOAD_CONST           (100)       1  CONSTANT              ('msg')
    6  CALL_FUNCTION        (131)       1  OTHER                 (1)
    9  STORE_FAST           (125)       0  LOCAL VARIABLE        (x)
   12  LOAD_FAST            (124)       0  LOCAL VARIABLE        (x)
   15  RETURN_VALUE         ( 83)
 
#---------- saia()
def saia():
    quit()
 
OFFSET INSTRUCTION          OPCODE    ARG  TYPE                  VALUE
    0  LOAD_GLOBAL          (116)       0  GLOBAL VARIABLE       (quit)
    3  CALL_FUNCTION        (131)       0  OTHER                 (0)
    6  POP_TOP              (  1)
    7  LOAD_CONST           (100)       0  CONSTANT              (None)
   10  RETURN_VALUE         ( 83)