Python & Django team trainer
I help folks sharpen their Python skills with https://PythonMorsels.com ππͺ
I also blog at https://treyhunner.com & run https://Whereabouts.Earth
PSF Fellow. YIMBY. 95% vegan.

@trey.io
Python & Django team trainer
I help folks sharpen their Python skills with https://PythonMorsels.com ππͺ
I also blog at https://treyhunner.com & run https://Whereabouts.Earth
PSF Fellow. YIMBY. 95% vegan.
βUsers of your custom classes will enjoy using your classes more when they're friendly.β
Read more π pym.dev/friendly-cla...
Note that each of those operations returns a set and sets are unordered. The next time you find yourself looping over one dictionary to check its keys against another dictionary (or a set), consider whether set arithmetic might help.
These operators also work between a dictionary's keys and a set: >>> expected = {"name", "email", "password"} >>> form_data = {"name": "Pat", "email": "[email protected]"} >>> expected - form_data.keys() {'password'}
This is equivalent to "set(east.keys()) & set(west.keys())", but without a set conversion. The dictionary keys method returns a set-like object, meaning the returned object supports set arithmetic. The other set operators work too (see tip #164): >>> east.keys() - west.keys() {'C95', 'F10'}
You might loop over one dictionary and check whether each key is ALSO in the other dictionary: shared = [] for code in east: if code in west: shared.append(code) That works, but there's a simpler way: >>> east.keys() & west.keys() {'D45', 'B38'}
Python Tip #339 (of 365):
Use set operations on dictionary keys. π§΅
Which products are stocked in BOTH of these warehouses?
>>> east = {"C95": 12, "D45": 3, "B38": 20, "F10": 7}
>>> west = {"A56": 8, "B38": 1, "E77": 4, "D45": 30}
I guessed all the countries right next to that pin (which were not the one the pin was in)
Whereabouts Daily
πΊοΈ December 5, 2026
5/5 countries in 8/13 guesses
π’π’π’π΅π’
whereabouts.earth/daily/
#Whereabouts
When I'm tempted to comment each block of a function, I consider whether it might be better written as multiple helper functions.
The name of each function does a pretty decent job of indicating the purpose of its code block. The arguments each function accepts also help us narrow down the purpose of specific variables to specific code blocks. And with separate functions we could even test each block on its own.
Here's the same code, broken up into separate functions:
def main():
args = parse_args()
subtitles_file = process_video(args.video_file)
move_subtitles(subtitles_file, args.video_file.parent)
Python Tip #338 (of 365):
Break commented blocks of code into separate functions. π§΅
Each comment here describes a different code block:
def main():
# Parse arguments
...
# Process video
...
# Move subtitles to final location
...
βThere's a common convention that text files should end with a newline character. The splitlines method is aware of this common convention and it will automatically remove a trailing newline character, if it finds one.β
Read more π pym.dev/splitlines/
Comments that describe what code does can OFTEN be refactored away. If a comment adds useful context that can't be easily discovered, DON'T remove it. So when in doubt, write a comment. But also don't be afraid to refactor that comment into something that's more understandable at a glance.
WHY code works a certain way is often "out of band" information, meaning it can't be inferred by reading the code: # AWS Lambda doesn't support Pool or Queue receiver, sender = multiprocessing.Pipe() Comments are for ADDING CONTEXT to your code.
Python Tip #337 (of 365):
Comment the "why" more than the "what". π§΅
WHAT code does (and even HOW it does it) can often be inferred by reading the code or the code's docstring.
Comments that describe WHY code operates a certain way are usually much more helpful.
πΊοΈ December 3, 2026
5/5 countries in 6/13 guesses
π’π’π’π‘π’
whereabouts.earth/daily/
#Whereabouts
Whenever I use "pass", I prefer to at least follow it with a comment saying WHY we're doing nothing: try: n = int(input("Guess a number: ")) except ValueError: pass # Non-number given: keep looping Also note that I'd prefer contextlib.suppress in this case (see tip #144).
If you have a class, function, or method with a body of just "pass", you can get rid of that "pass" by replacing it with a docstring. In Python, "pass" means DO NOTHING. If you're going to write a statement that does nothing, it may as well act as documentation.
Every block of code needs at least one statement in it. That "pass" statement was needed because the colon before it started a code block and we had nothing to put in the block. But a docstring is a valid statement too (and a more helpful one than "pass").
Python Tip #336 (of 365):
Replace "pass" with a docstring. π§΅
Instead of this:
class BillingError(Exception):
pass
Do this:
class BillingError(Exception):
"""Raised when a credit card can't be charged."""