Trey Hunner π
@treyhunner@mastodon.social
#Python & #Django educator & team trainer
I help folks sharpen their Python skills with https://pythonmorsels.comππͺ
Also: ostrovegan, sentientist, YIMBY, and I think economics is highly underrated. I don't post about any of those topics very often.
he/him
- π My Weekly Newsletter
- https://pym.dev/newsletter
- π Python Exercises
- https://www.pythonmorsels.com
- πΊ YouTube
- https://www.youtube.com/@PythonMorsels
- πΈ Personal Blog
- https://treyhunner.com
Recent public posts
- Python Tip #319 (of 365): Name your regular expression capture groups. Capture groups are neat, but it can be confusing figuring out what the group numbers repr...
- Python Tip #315 (of 365): Don't reach for random.random. Need a random integer? Don't multiply a random float and convert the result. The randint function does ...
- βThe Car class isn't technically a subclass of Goose, but it's seen as a sort of virtual subclass.β Read more π https://pym.dev/goose-typing/ #Python
- Python Tip #313 (of 365): Don't use match-case as a switch statement. Do this: if day in ("saturday", "sunday"): return "weekend" else: return "weekday" Not thi...
- Python Tip #312 (of 365): Use the "utf-8-sig" encoding for CSV files. Many CSV files start with an invisible byte order mark (BOM). When read as "utf-8", the fi...
- βPython 3.15 also has a big syntax addition: explicit lazy imports.β Read more π https://pym.dev/python315/ #Python
- Python Tip #311 (of 365): Make directories with parents and exist_ok. Like this: pathlib.Path("reports/2026").mkdir(parents=True, exist_ok=True) Without "parent...
- Python Tip #309 (of 365): Match dictionaries by their keys with match-case. >>> response = {"type": "rectangle", "width": 3, "height": 4} >>> match response: .....
- Python Tip #308 (of 365): Match tuple-like data by shape with match-case. >>> point = (3, 4) >>> match point: ... case (x, y): ... print(f"2D point ({x}, {y})")...
- Python Tip #305 (of 365): Split only as much as you need with maxsplit. Do this: key, value = line.split("=", maxsplit=1) Not this: key, *rest = line.split("=")...
- Apparently it's vegan awareness day. Are you aware that I am vegan? I'm always open to replies/messages/DMs about food production. I'm not an expert on the ethi...
- We have an election in California in a couple days... which means I spent my Sunday trying to moonlight as a legislator, researching the 14 propositions we have...
- I just launched a new Python Morsels course: Lazy Looping in Python π Python's "for" loops can only loop forward one item at a time, so we use looping helpers ...
- Python Tip #300 (of 365): Separate the steps of a function with blank lines. If you're using an auto-formatter like ruff or Black, you won't have control over m...
- Python Tip #299 (of 365): Return Boolean expressions directly. Do this: def is_even(n): return n % 2 == 0. Not this: def is_even(n): if n % 2 == 0: return True ...
- Apparently there are 28 Pythagorean triple dates each century and we just missed celebrating the last one for the century (yesterday!). >>> from itertools impor...
- Python Tip #298 (of 365): When creating lots of class instances, save memory with __slots__. class Point: __slots__ = ("x", "y", "z") def __init__(self, x, y, z...
- Python Tip #293 (of 365): Write context managers with contextlib.contextmanager. Instead of making a class with __enter__ and __exit__ methods, we can make a co...
- Python Tip #292 (of 365): Keep "try" blocks small with an "else" clause. try: count = int(text) except ValueError: print("That's not a number") else: print(f"Co...
- Python Tip #290 (of 365): Pretty-print data structures with pp. >>> from pprint import pp >>> pp(user) {'name': 'Trey', 'languages': ['Python', 'JavaScript'], '...