Deconstructing Interpreter: Understanding Behind the Python Bytecode
Last Updated :
10 May, 2020
When the CPython interpreter executes your program, it first translates onto a sequence of bytecode instructions. Bytecode is an intermediate language for the Python virtual machine that’s used as a performance optimization.

Instead of directly executing the human-readable source code, compact numeric codes, constants, and references are used that represent the result of compiler parsing and semantic analysis. This saves time and memory for repeated executions of programs or part of programs. For example, the bytecode resulting from this compilation step is cached on disk in .pyc and .pyo files so that executing the same Python file is faster the second time around. All of this is completely transparent to the programmer. You don’t have to be aware that this intermediate translation step happens, or how the Python virtual machine deals with the bytecode. In fact, the bytecode format is deemed an implementation detail and not guaranteed to remain stable or compatible between Python versions. And yet, one may find it very enlightening to see how the sausage is made and to peek behind the abstractions provided by the CPython interpreter. Understanding at least some of the inner workings can help you write more performant code.
Example: Let’s take this simple
showMeByte() function as a lab sample and Understand’s Python’s bytecode:
Python3 1==
def showMeByte(name):
return "hello "+name+" !!!"
print(showMeByte("amit kumra"))
Output:
hello amit kumra !!!
CPython first translates our source code into an intermediate language before it runs it. We can see the results of this compilation step. Each function has a
__code__
attribute(in Python 3) that we can use to get at the virtual machine instructions, constants, and variables used by our showMeByte function:
Example:
Python3 1==
def showMeByte(name):
return "hello "+name+" !!!"
print(showMeByte.__code__.co_code)
print(showMeByte.__code__.co_consts)
print(showMeByte.__code__.co_stacksize)
print(showMeByte.__code__.co_varnames)
print(showMeByte.__code__.co_flags)
print(showMeByte.__code__.co_name)
print(showMeByte.__code__.co_names)
Output:
b'd\x01|\x00\x17\x00d\x02\x17\x00S\x00'
(None, 'hello ', ' !!!')
2
('name',)
67
showMeByte
()
You can see co_consts contains parts of the greeting string our function assembles. Constants and code are kept separate to save memory space. So instead of repeating the actual constant values in the co_code instruction stream, Python stores constants separately in a lookup table. The instruction stream can then refer to a constant with an index into the lookup table. The same is true for variables stored in the co_varnames field. CPython developers gave us another tool called a disassembler to make inspecting the bytecode easier. Python’s bytecode disassembler lives in the dis module that’s part of the standard library. So we can just import it and call
dis.dis()
on our greet function to get a slightly easier-to-read representation of its bytecode:
Example:
Python3 1==
import dis
def showMeByte(name):
return "hello "+name+" !!!"
dis.dis(showMeByte)
bytecode = dis.code_info(showMeByte)
print(bytecode)
bytecode = dis.Bytecode(showMeByte)
print(bytecode)
for i in bytecode:
print(i)
Output:

The executable instructions or simple instructions tell the processor what to do. Each instruction consists of an operation code (opcode). Each executable instruction generates one machine language instruction. The main thing disassembling did was split up the instruction stream and give each opcode in it a human-readable name like LOAD_CONST. You can also see how constant and variable references are now interleaved with the bytecode and printed in full to spare us the mental gymnastics of a co_const and co_varnames table lookup.
It first retrieves the constant at index 1(‘Hello’) and puts it on the stack. It then loads the contents of the name variable and also puts them on the stack. The stack is the data structure used as internal working storage for the virtual machine. There are different classes of virtual machines and one of them is called a stack machine. CPython’s virtual machine is an implementation of such a stack machine. CPython’s virtual machine is an implementation of such a stack machine. Let’s assume the stack starts out empty. After the first two opcodes have been executed, this is what contents of the VM look like(0 is the topmost element):
0: ’amit kumra’(contents of “name”)
1: ‘hello ‘
The
BINARY_ADD instruction pops the two string values off the stack, concatenation them, and then pushes the result on the stack again:
0: ‘hello amit kumra’
Then there’s another LOAD_CONST to get the exclamation mark string on the stack:
0 : ‘ !!!’
1:’Hello amit kumra’
The next BINARY_ADD opcode again combines the two to generate the final greeting string:
0: ‘hello amit kumra !!!’
The last bytecode instruction is
RETURN_VALUE which tells the virtual machine that what’s currently on top of the stack is the return value for this function so it can be passed on to the caller. So, finally, we traced back how our
showMeCode()
function gets executed internally by the CPython virtual machine.
Similar Reads
Best Python Interpreters: Choose the Best in 2024 Python, known for its versatility and ease of use, offers developers a range of interpreters to suit various needs and preferences. Whether you're aiming for performance, compatibility, or integration with specific platforms, understanding the available options is essential. In this guide, we'll exp
3 min read
Encoding and Decoding Base64 Strings in Python The Base64 encoding is used to convert bytes that have binary or text data into ASCII characters. Encoding prevents the data from getting corrupted when it is transferred or processed through a text-only system. In this article, we will discuss about Base64 encoding and decoding and its uses to enco
4 min read
base64.decodestring(s) in Python With the help of base64.decodestring(s) method, we can decode the binary string with the help of base64 data into normal form. Syntax : base64.decodestring(b_string) Return : Return the decoded string. Example #1 : In this example we can see that by using base64.decodestring(s) method, we are able t
1 min read
Convert Unicode to Bytes in Python Unicode, often known as the Universal Character Set, is a standard for text encoding. The primary objective of Unicode is to create a universal character set that can represent text in any language or writing system. Text characters from various writing systems are given distinctive representations
2 min read
How To Encode And Decode A Message using Python? Encryption is the process of converting a normal message (plain text) into a meaningless message (Ciphertext). Whereas, Decryption is the process of converting a meaningless message (Cipher text) into its original form (Plain text). In this article, we will take forward the idea of encryption and de
3 min read
How to Convert Int to Bytes in Python? The task of converting an integer to bytes in Python involves representing a numerical value in its binary form for storage, transmission, or processing. For example, the integer 5 can be converted into bytes, resulting in a binary representation like b'\x00\x05' or b'\x05', depending on the chosen
2 min read
Python Interview Questions and Answers Python is the most used language in top companies such as Intel, IBM, NASA, Pixar, Netflix, Facebook, JP Morgan Chase, Spotify and many more because of its simplicity and powerful libraries. To crack their Online Assessment and Interview Rounds as a Python developer, we need to master important Pyth
15+ min read
Execute a String of Code in Python Sometimes, we encounter situations where a Python program needs to dynamically execute code stored as a string. We will explore different ways to execute these strings safely and efficiently.Using the exec function exec() function allows us to execute dynamically generated Python code stored in a st
2 min read
base64.standard_b64decode(s) in Python With the help of base64.standard_b64decode() method, we can decode the binary string using base64 alphabets into normal form of strings. Syntax : base64.standard_b64decode(b_string) Return : Return the decoded string. Example #1 : In this example we can see that by using base64.standard_b64decode()
1 min read
Convert Unicode String to a Byte String in Python Python is a versatile programming language known for its simplicity and readability. Unicode support is a crucial aspect of Python, allowing developers to handle characters from various scripts and languages. However, there are instances where you might need to convert a Unicode string to a regular
2 min read