Lab 01: CPython Internals
Overview
Step 1: The PyObject Model
import sys
x = "hello"
print(sys.getrefcount(x)) # Always +1 because getrefcount itself holds a ref
a = x
b = x
print(sys.getrefcount(x)) # +2 more
del a
print(sys.getrefcount(x)) # back down by 1
# Object identity
print(id(x)) # memory address of PyObject
print(type(x)) # ob_type -> str
print(x.__class__) # sameStep 2: The dis Module — Bytecode Disassembly
dis Module — Bytecode DisassemblyStep 3: Code Objects
Step 4: Frame Objects
Step 5: sys.getsizeof — Object Memory Layout
sys.getsizeof — Object Memory LayoutStep 6: Bytecode Opcodes Deep Dive
Step 7: The Peephole Optimizer
Step 8: Capstone — Bytecode Analyzer Tool
Summary
Concept
Tool/API
Use Case
Last updated
