Day 3
Day 3
• What is Bytecode?
• How Python runs code
• Using the dis module
• How CPython works
• Simple code demo.
• Q&A
Learning Objectives
Technical Definition
• Think of bytecode as Python's "secret language" - it's the
intermediate code that Python creates from your human-readable
Python code before actually running it.
• Bytecode is a set of instructions Python creates from your code.
• It's a middle step between code you write and code that runs.
• Saved as .pyc files in __pycache__.
• Bytecode is a low-level, platform-independent representation of
your Python source code that consists of instruction sequences for
the Python Virtual Machine (PVM)
Python's dis Module
• The `dis` module is Python's built-in disassembler that lets you peek under the
hood and see the bytecode instructions that Python generates from your code.
• dis shows bytecode instructions for a function
• The dis module provides functions to analyze CPython bytecode by
disassembling functions, methods, classes, and code objects into human-
readable instruction sequences.
• It reveals the stack-based operations that the CPython interpreter performs.
CPython Internals
import dis
def greet(name):
return "Hi " + name
dis.dis(greet)
How Python Runs Code
THANK YOU