The python programming language includes an in-built module called dis which can be used to disassemble the python code to human-readable format.
What is disassembling code?
Disassembling is nothing but converting the code to a more human readable format. This is usually done with the help of a disassembler. A disassembler may be stand-alone or interactive.
Let us get started with a small example. Consider the following function which adds two numbers let us look at what the dis module does to the following code.
The addition function.
def add(num1, num2): return num1 + num2
Let us import the module and disassemble it.
If we save this code and run it, the following data will be printed on our console.
The output is organised into columns with the original source line number, the instruction “address” within the code object, the opcode name, and any arguments passed to the opcode.
We can see what happens here. It is more readable now. The following things happen in the add() function.
- Loads num1
- Loads num2
- Performing binary addition
- Returning the value
Hence, the dis module can be used to disassemble the code. They provide many more functions than this. Check it out in their official documentation here.