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 :