Bytecode and .pyc files
summary
Explains that CPython compiles source to bytecode (about 122 instructions in 3.12) stored in .pyc files under __pycache__ with a magic number and timestamp/hash; shows how to inspect and mutate code with dis, marshal and types.FunctionType, uses code.replace to swap opcodes (example: changing a binary-op yields unexpected numeric results, discovered as BINARY_AND → 2), shows python -m compileall and -O levels (asserts/docstrings removed), and notes ~100 ms startup win for precompiled bytecode and poorer tracebacks if .py files are removed.
AI-written summary · about
chapters (23)
- 0:00Introduction and speaker intro
- 1:10Speaker introduction
- 2:39Connectivity check and talk start
- 3:00What is Python bytecode
- 4:45How Python interpreter works
- 6:34Bytecode instructions overview
- 8:18Tools for reading bytecode
- 11:59Deep dive into bytecode details
- 15:18Frames and stacks in Python
- 16:46Interpreting bytecode step by step
- 20:21Introduction to .pyc files
- 22:10Creating and using .pyc files
- 23:56Optimizing with .pyc files
- 25:50Bytecode optimization levels
- 26:34Exception handling in bytecode
- 27:40Modifying functions on the fly
- 29:10Advanced bytecode manipulation
- 31:20Q&A session begins
- 31:41Bytecode optimizations explained
- 32:24Practical takeaways
- 33:28Null values in bytecode
- 34:13JIT compilation in Python
- 35:15Performance impact of .pyc files
