Grilled Cheese

ExploreLog inSign up

Trey Hunner

@trey.io

219 Following4k Followers

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.

PostsRepliesMedia
@trey.ioDec 6, 2026, 12:05 AM

β€œUsers of your custom classes will enjoy using your classes more when they're friendly.”

Read more πŸ‘‰ pym.dev/friendly-cla...

#Python

pym.devhttps://pym.dev/friendly-classes/
@trey.ioDec 5, 2026, 9:01 PM

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.

@trey.ioDec 5, 2026, 9:01 PM

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'}

@trey.ioDec 5, 2026, 9:01 PM

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'}

bsky.appTrey Hunner (@trey.io)Python Tip #164 (of 365): Thinking in terms of a Venn diagram? You need a set. 🧡 Need to find all items in one collection but not another? Intersect two collections? Check what's unique to each?…
@trey.ioDec 5, 2026, 9:01 PM

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'}

@trey.ioDec 5, 2026, 9:01 PM

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}

#Python #DailyPythonTip

pym.devPractical uses of setsSets are unordered collections of values that are great for removing duplicates, quick containment checks, and set operations.
@trey.ioDec 5, 2026, 4:52 PM

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

whereabouts.earthWhereabouts DailyHow many of today's five countries can you guess?
@trey.ioDec 4, 2026, 9:03 PM

When I'm tempted to comment each block of a function, I consider whether it might be better written as multiple helper functions.

pym.devAvoid over-commenting in PythonWhen do you need a comment in Python and when should you consider an alternative to commenting?
@trey.ioDec 4, 2026, 9:03 PM

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.

@trey.ioDec 4, 2026, 9:03 PM

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)

@trey.ioDec 4, 2026, 9:03 PM

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
...

#Python #DailyPythonTip

@trey.ioDec 4, 2026, 8:22 PM

β€œ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/

#Python

pym.devWhy splitlines() instead of split("\n")?To split text into lines in Python, use the splitlines() method, not the split() method.
@trey.ioDec 3, 2026, 9:02 PM

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.

pym.devAvoid over-commenting in PythonWhen do you need a comment in Python and when should you consider an alternative to commenting?
@trey.ioDec 3, 2026, 9:02 PM

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.

@trey.ioDec 3, 2026, 9:02 PM

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.

#Python #DailyPythonTip

@trey.ioDec 3, 2026, 2:23 PM

πŸ—ΊοΈ December 3, 2026
5/5 countries in 6/13 guesses
🟒🟒🟒🟑🟒
whereabouts.earth/daily/
#Whereabouts

whereabouts.earthWhereabouts DailyHow many of today's five countries can you guess?
@trey.ioDec 2, 2026, 9:03 PM

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).

bsky.appTrey Hunner (@trey.io)Python Tip #144 (of 365): To suppress specific exceptions, use contextlib.suppress 🧡 You could suppress an exception like this: try: ... except ValueError: pass But I would recommend this: from…
@trey.ioDec 2, 2026, 9:03 PM

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.

@trey.ioDec 2, 2026, 9:03 PM

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").

@trey.ioDec 2, 2026, 9:03 PM

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."""

#Python #DailyPythonTip

Older posts
Terms of UsePrivacy PolicyCommunity StandardsHelpGet the app

Grilled Cheese is a product of Village Compute

Version devBuilt at: 2026-12-03 22:42:33 EST