A translator is the one which translates the source code into object code.

Translator which convert high level language into a byte code which is further converted into machine understandable format by a Compiler.

Translator which implicitly convert the source program line by line into byte code and interpret the code at same time without the need for explicit byte code generation by Interpreter.

In python, the byte code is generated internally by PVM(python virtual machine) in the memory heap which is not visible to programmers eyes.

There are different types of compiler that are used for python

  • CPython(C compiler)
  • PyPy(JIT compiler)

JIT compiler is faster compared to C compiler during interpretation phase.

Below example in linux shows the procedure to peep through the byte code files generated by python3 compiler.

Here python3 calls the compiler to display the results directly to the screen rather than generating the byte code files.

Create a test code called

[hydra@hydrupgrd hydrapy]$ cat


Execute the python file using python3 to display the output

[hydra@hydrupgrd hydrapy]$ python3

There is a module called which prints the byte code information into .pyc file which is not readable using editor. Also man page provides some info about the options that can be used to generate the byte code

[hydra@hydrupgrd hydrapy]$ man python3
       -m module-name
              Searches sys.path for the named module and runs the corresponding .py file as a script.

[hydra@hydrupgrd hydrapy]$ locate

Generate the byte code with the following command and a new directory called __pycache__ will be created

[hydra@hydrupgrd hydrapy]$ python3 -m py_compile
[hydra@hydrupgrd hydrapy]$ ls -lrt|tail -1
drwxr-xr-x 2 hydra hydra   33 Nov 24 17:57 __pycache__
[hydra@hydrupgrd hydrapy]$ cd __pycache__/
[hydra@hydrupgrd __pycache__]$ ls

Check the type of the file which shows byte compiled.

[hydra@hydrupgrd __pycache__]$ file test.cpython-36.pyc
test.cpython-36.pyc: python 3.6 byte-compiled <<<

To display the contents of the file, use the below methods

[hydra@hydrupgrd __pycache__]$ strings test.cpython-36.pyc
[hydra@hydrupgrd __pycache__]$ cd ..

To display all the internal function calls used by compiler, use the dis option

[hydra@hydrupgrd hydrapy]$ python3 -m dis
  4           0 LOAD_NAME                0 (print)
              2 LOAD_CONST               0 ('Hello')
              4 CALL_FUNCTION            1
              6 POP_TOP
              8 LOAD_CONST               1 (None)
             10 RETURN_VALUE

Leave a Reply