Let us begin this week with some PYTHON -
Have you ever wondered what Python does under the hood when you run a program?
With “dis()” you can view the formatted bytecode operations happening for any function / Class / code object.
Bytecode is a internal version-specific implementation detail of the interpreter.
Python is often described as an interpreted language in which the source code is translated into CPU instructions as the program runs but this is only partially correct.
Python actually compiles source code to a set of instructions for a virtual machine and the Python interpreter is an implementation of that virtual machine.
Those “.pyc” (in case of python 2.x) files are the bytecode instructions that will be executed by Python’s virtual machine as your program runs.
With python 3.x a “__pycache__ "directory is created for the source and all compiled Bytecode files are placed underneath.
In the CPython interpreter, Python code is first converted to an intermediate representation, the bytecode, and then executed by the Python interpreter.
The dis module in Python standard library provides various functions useful for analysis of Python bytecode by disassembling it into a human-readable form
- It prints one line per bytecode instruction
- It also recursively disassembles nested code objects
This helps to perform optimizations. Look at an example below and things will become clear for you :