PythonMastery
Tips & tricks

Python tips that change how you write code

Short, practical, and every one runnable: the before, the after, why it works, and when not to use it. Every example on these pages is run when the site is built, so the output you see is the output Python gives.

In the wild

real projects, the decision that mattered, and the tip you can take from it
DropboxHow Dropbox moved its desktop app to Python 3The tip: Migrate in stages, behind a switch you can turn back, and fix every regression before widening the rolloutDropboxHow Dropbox type-checked 4 million lines of PythonThe tip: Add type hints gradually, starting where code is hardest to readCPythonPython 3.11: an average 25% faster without changing your codeThe tip: Upgrade Python before you rewrite code for speed, and stop avoiding try blocks for performanceCPythonPre-fork Python servers: how Python stopped wasting their shared memoryThe tip: In a pre-fork server, build shared state once in the parent, then gc.freeze() it before forkingSpotifyHow Spotify runs thousands of pipeline tasks a day with LuigiThe tip: Make each pipeline step skip work whose output already exists, and only publish finished results

Lessons from incidents

what went wrong, why, and the habit that would have caught it
CloudflareThe regular expression that took Cloudflare down for 27 minutesThe tip: Before a regex meets untrusted input, time it on a long string that nearly matches but doesn'tGitLabGitLab deleted its production database, and the backups weren't thereThe tip: A backup you have never restored is a hope, not a backup: restore it automatically and check what comes backPyPIThe PyPI package whose GitHub repository was cleanThe tip: Pin exact versions with hashes, so a new release can't reach your machine until you choose to take it

Did you know?

Python behaviour that surprises people who have used it for years.

Idioms

The shorter, clearer way experienced Python developers write it.

Industry practice

What working teams do, and why they bother.

Standard-library gems

Already installed, and replaces code you were about to write.

Performance

Measured, not guessed: run the timing yourself.

Debugging

Find the problem faster than print() and hope.