Saturday 4 November 2017

Python view Byte Code for the given script

Python view Byte Code for the given script


Normal execution syntax
python file_name.py

For retrieving the Byte Code information the execution syntax is,
python -m dist file_name.py

which will display in a disassembler format (human readable format)

Example :

# simple.py
a=10
b=20
c=a+b
print(c)

#output 
30


#cat bytecode.py

a=10
b=20
c=a+b
print(c)

#python3.6 bytecode.py

30

#python3.6 -m dis bytecode.py

  1           0 LOAD_CONST               0 (10)
              2 STORE_NAME               0 (a)

  2           4 LOAD_CONST               1 (20)
              6 STORE_NAME               1 (b)

  3           8 LOAD_NAME                0 (a)
             10 LOAD_NAME                1 (b)
             12 BINARY_ADD
             14 STORE_NAME               2 (c)

  4          16 LOAD_NAME                3 (print)
             18 LOAD_NAME                2 (c)
             20 CALL_FUNCTION            1
             22 POP_TOP
             24 LOAD_CONST               2 (None)
             26 RETURN_VALUE



No comments:

Post a Comment