Technical
The Python Standard Library Tricks I Should Have Learned Sooner
Every dependency is a future breakage. Every standard library call is a future guarantee. This year I deliberately reached for the stdlib first. The education was humbling.
Trick 1: pathlib over os.path
pathlib is fluent, cross-platform, and obvious. os.path.join is not. I have not written os.path.anything in months.
from pathlib import Path
config = Path.home() / '.config' / 'app.toml'
if config.exists():
text = config.read_text()Trick 2: dataclasses with slots
from dataclasses import dataclass
@dataclass(slots=True, frozen=True)
class User:
id: int
email: strslots and frozen together give you immutable, memory-efficient records without a framework.
Trick 3: functools.lru_cache for pure functions
A decorator and the function is memoized. Useful for parse-once config loads, expensive pure computations, and repeated lookups.
Trick 4: contextlib.contextmanager for quick context managers
I used to define enter and exit classes. contextlib.contextmanager is two lines and generator syntax. Readable and enough for 95 percent of cases.
Trick 5: collections.Counter for frequency analysis
from collections import Counter
top_categories = Counter(tags).most_common(5)I used to hand-write the dict incrementing. Never again.
Trick 6: itertools.batched in Python 3.12
Batch any iterable into chunks of N. Previously required a helper function. Now one import.
Trick 7: subprocess.run with check=True and capture_output=True
The default I should have learned early. Any other subprocess pattern is almost always wrong.
Trick 8: datetime with zoneinfo
Python 3.9 added zoneinfo to the stdlib. Good riddance to pytz.
from datetime import datetime
from zoneinfo import ZoneInfo
now_beirut = datetime.now(ZoneInfo('Asia/Beirut'))Trick 9: argparse for real CLI scripts
Before adding click or typer, check whether argparse covers the need. It almost always does.
Trick 10: http.server for local static serving
One command, one port, done. I no longer install a package for this.
Reading
Python stdlib docs are remarkably readable. Spend an afternoon there and save a dozen dependencies.
RELATED READING
The Consulting Shift I Am Making In Year Two
After a year of writing and building, my consulting practice is changing shape. Shorter engagements. Sharper outcomes.
ReadThe Frontend Shift: Shipping Less JavaScript In Year Two
A year ago I reached for Next.js for everything. This year I often reach for nothing.
ReadThe Serverless Lesson I Would Write On A Sticky Note
After a year of shipping serverless projects, one rule explains most of the wins and all of the losses.
Read