Showing posts with label Basics. Show all posts
Showing posts with label Basics. Show all posts

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



Friday, 3 November 2017

How to create compiled files in python (py to pyc conversion)

How to create compiled files in python (py to pyc conversion)

This post explains about generating compiled files in python(py to pyc conversion).

The syntax is given as follows,

python -m py_compile file_name.py

where,
python is the executor command
-m is to represent the module
py_compile is the module to be called
file_name.py is the actual file which has to be compiled

This will create a output file like file_name.cpython.pyc