Python is one of the most popular programming languages due to its simplicity and readability. But have you ever wondered how Python actually works under the hood? In this post, we will explore Python’s internals, including its execution model, memory management, and how it handles functions, loops, and conditions.
Table of contents
Open Table of contents
Python Execution Model
When you write a Python program, it goes through multiple stages before running. Let’s break it down step by step.
1. Lexing and Tokenization
Python first converts your code into tokens. Consider the following code snippet:
x = 10 + 5print(x)Python breaks this down into tokens like:
NAME 'x'OPERATOR '='NUMBER '10'OPERATOR '+'NUMBER '5'NAME 'print'PUNCTUATION '('NAME 'x'PUNCTUATION ')'2. Parsing and AST Generation
After tokenization, Python parses the tokens and generates an Abstract Syntax Tree (AST).
You can visualize the AST using Python:
import astcode = "x = 10 + 5"ast_tree = ast.parse(code)print(ast.dump(ast_tree, indent=4))3. Bytecode Compilation
Python compiles the AST into bytecode, a lower-level representation that the Python Virtual Machine (PVM) understands.
To see bytecode, use:
import discode = "x = 10 + 5"compiled_code = compile(code, '<string>', 'exec')dis.dis(compiled_code)4. Execution by Python Virtual Machine (PVM)
The Python Virtual Machine (PVM) interprets the bytecode and executes it step by step.
Memory Management in Python
Python uses a combination of reference counting and garbage collection to manage memory efficiently.
Reference Counting
Every object in Python has a reference count. When an object’s reference count drops to zero, Python deallocates it.
import sysx = [1, 2, 3]
print(sys.getrefcount(x))Garbage Collection
Python periodically runs a garbage collector to clean up unreferenced objects.
import gcgc.collect()How Python Handles Functions
Functions in Python are first-class objects, meaning they can be assigned to variables, passed as arguments, and returned from other functions.
def greet(): return "Hello, World!"
hello = greetprint(hello())Understanding Loops and Conditions
Loops
Python supports for and while loops.
for i in range(5): print(i)Conditions
Python uses if, elif, and else for decision-making.
x = 10if x > 5: print("x is greater than 5")elif x == 5: print("x is equal to 5")else: print("x is less than 5")Conclusion
Python’s execution model, memory management, and handling of functions and loops make it a powerful yet easy-to-use language. Understanding these internals will help you write more efficient and optimized Python programs.