# Python Language Research ā Index
> Compiled May 2025. Comprehensive research on Python internals, features, and best practices.
---
## Documents
### 1. [Python_Recent_Features.md](Python_Recent_Features.md)
Python 3.10ā3.13 features with code examples.
**Key highlights:**
- **3.10:** Pattern matching (`match`/`case`), `str | int` union syntax, parenthesized context managers
- **3.11:** Exception groups (`except*`), finer-grained error messages, self-documenting assertions, `tomllib`
- **3.12:** Free-threading (no-GIL) preview, f-string improvements, performance +10-25%
- **3.13:** Type parameter syntax (`def f[T]`), type aliases (`type X =`), per-interpreter GIL, compile-time f-strings, static assertions
### 2. [Python_Advanced_Features.md](Python_Advanced_Features.md)
Metaclasses, descriptors, generators, MRO, and dark patterns.
**Topics covered:**
- Metaclasses: auto-registration, API enforcement
- Descriptors: type validation, cached properties
- `__slots__`: memory optimization (63% savings)
- MRO and C3 linearization
- `__new__` vs `__init__`: singletons, factories, immutable types
- Generator tricks: `.send()`, `.throw()`, `yield from`
- Python gotchas: mutable defaults, late binding closures, integer interning
- The GIL and multiprocessing workarounds
- Memory management: ref counting + garbage collection
- Common anti-patterns: bare except, modifying during iteration, `eval()` misuse
### 3. [Python_Lesser_Known_Modules.md](Python_Lesser_Known_Modules.md)
16 underutilized standard library modules.
**Modules covered:**
| Module | Use Case |
|--------|----------|
| `itertools` | Iterator combinatorics |
| `contextlib` | Resource management (ExitStack, suppress) |
| `weakref` | Non-owning references, caching |
| `inspect` | Runtime introspection |
| `dis` | Bytecode analysis |
| `enum` | Type-safe constants (Flag, IntEnum) |
| `functools` | Polymorphism (singledispatch), caching |
| `mmap` | Large file I/O |
| `sched` | Event scheduling |
| `difflib` | Text comparison |
| `struct` | Binary data packing |
| `shlex` | Shell parsing |
| `tracemalloc` | Memory profiling |
| `atexit` | Cleanup handlers |
| `lzma` | Compression |
| `dataclasses` | Advanced features (frozen, slots, kw_only) |
### 4. [Python_Performance_Tips.md](Python_Performance_Tips.md)
Deep dive into Python internals and optimization.
**Topics covered:**
- Local vs global variable lookup (`LOAD_FAST` vs `LOAD_GLOBAL`)
- `__slots__` memory optimization
- List comprehension vs `map`/`filter`
- String concatenation (`"".join()` vs `+=`)
- Dict/set lookups ā O(1) average
- Generator memory savings
- String interning
- `collections` performance (Counter, defaultdict, deque)
- Function call overhead
- Memory profiling with `tracemalloc`
- Caching and memoization (`lru_cache`, `cached_property`)
- I/O performance (buffering, mmap, async)
---
## Quick Reference: Python Gotchas
```python
# 1. Mutable default arguments
def bad(item, target=[]): # SHARED across calls!
target.append(item)
def good(item, target=None): # Create new each time
if target is None: target = []
# 2. Late binding closures
bad = [lambda: i for i in range(5)] # All return 4
good = [lambda i=i: i for i in range(5)] # Return 0,1,2,3,4
# 3. Integer interning ā don't use `is` for values
a = 257; b = 257
print(a is b) # True (implementation-dependent)
print(a == b) # True (always correct)
# 4. Truthiness ā 0 is falsy but valid
if score: # FAILS when score == 0
if score is not None: # Correct
# 5. Modifying during iteration
for k in d: del d[k] # RuntimeError!
for k in list(d): del d[k] # Safe
# 6. Chained assignment with mutables
x = y = []
x.append(1)
print(y) # [1] ā same list!
```
---
## Quick Reference: Performance Tips
```python
# 1. Cache globals in locals
def fast():
local_pi = PI # LOAD_GLOBAL once
return [local_pi * r * r for r in radii] # LOAD_FAST
# 2. Use sets for membership testing
data_set = set(range(1_000_000)) # O(1) lookup vs O(n) for list
# 3. String joining
"".join(str(i) for i in range(n)) # O(n)
result += str(i) # O(n²) in loop!
# 4. __slots__ for memory
class Point:
__slots__ = ('x', 'y') # 63% memory savings
# 5. Generator for large data
(i*i for i in range(1_000_000)) # O(1) memory
[i*i for i in range(1_000_000)] # O(n) memory
# 6. Caching
@lru_cache(maxsize=128)
def expensive(n): ...
@cached_property
def computed(self): ...
```
---
## Research Notes
### What I Learned
1. **Pattern matching** is more powerful than I realized ā it's not just switch/case, it's full structural decomposition with guards.
2. **Type parameter syntax** (`def f[T]`) in Python 3.13 is a huge simplification over `TypeVar`.
3. **Free-threading** is coming to CPython ā the GIL is finally being addressed.
4. **Descriptors** are everywhere ā properties, classmethods, staticmethods, and even `@cached_property` all use them.
5. **`weakref`** has so many practical uses: caching without memory leaks, observer patterns, avoiding reference cycles.
6. **`contextlib.ExitStack`** is underutilized ā perfect for dynamic resource management.
7. **`dis` module** is invaluable for understanding Python internals and performance.
8. **`mmap`** can make file I/O dramatically faster for large files.
9. **The GIL** is less problematic than I thought ā I/O-bound code benefits from threading, and multiprocessing handles CPU-bound work.
10. **Python 3.13+** has per-interpreter GIL ā multiple interpreters running in parallel within the same process.
### Surprising Facts
- Python caches integers from -5 to 256 (`sys.intern()` for strings in 3.12+)
- `LOAD_FAST` is ~2x faster than `LOAD_GLOBAL`
- `__slots__` saves ~63% memory per instance
- Set lookups are ~500x faster than list membership for large collections
- String concatenation in a loop is O(n²) ā use `"".join()`
- `functools.singledispatch` gives you polymorphism without inheritance
- `itertools.tee()` creates independent iterators from one source
- `difflib.SequenceMatcher` can compute similarity ratios
- `sched` provides a built-in priority queue scheduler
- `tracemalloc` is built-in memory profiling
---
## Future Research Topics
- AsyncIO deep dive (event loops, tasks, channels)
- CPython internals (GIL implementation, bytecode, optimization pipeline)
- Type system deep dive (Protocol, TypeGuard, TypeIs)
- Packaging and distribution (PEP 517, 518, build systems)
- Performance comparison: CPython vs PyPy vs GraalPy
- Memory models and the garbage collector in detail
- Cython and C extensions for performance-critical code