βThe utilities in the textwrap module are meant to operate on text that represents just one paragraph.β Read more π pym.dev/wrapping-text/ #Python
Trey Hunner
Trey Hunner
@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.
4K followers219 following2.4K posts
πΊοΈ October 27, 2026 5/5 countries in 6/13 guesses π’π’π’π’π‘ whereabouts.earth/daily/ #Whereabouts
So use bool to get True or False: def is_number(value): return bool(re.fullmatch(r"-?\d+(\.\d+)?", value)) Whether bool(...) is clearer than an "if"/"else" is a bit more subjective, but I usually find it clearer. When I see bool(something) I think "we're performing a truthiness check".
βThe print function is handy for showing output at the terminal, but that's not its only use.β Read more π pym.dev/print-featur... #Python
πΊοΈ October 26, 2026 5/5 countries in 6/13 guesses π’π’π‘π’π’ whereabouts.earth/daily/ #Whereabouts
βSomething to keep in mind is that sys.platform actually represents the operating system that your Python interpreter was built for, while platform.system() returns the operating system that your Python interpreter is currently running on.β Read more π pym.dev/operating-sy... #Python
Apparently there are 28 Pythagorean triple dates each century and we just missed celebrating the last one for the century (yesterday!). These 8 plus 20 permutations: 2005-04-03 2010-08-06 2013-12-05 2015-12-09 2017-08-15 2020-12-16 2025-07-24 2026-10-24 Better luck next century.
For millions of instances of a class, __slots__ is definitely worth it. But for just a handful of objects, don't bother with __slots__.
πΊοΈ October 25, 2026 5/5 countries in 5/13 guesses π’π’π’π’π’ whereabouts.earth/daily/ #Whereabouts
Trey Hunner reposted
@trey.io telling us everything and more about Python classes. #pybeach #pybeach2026
βWhen it comes to code readability, whitespace is your friend.β Read more π pym.dev/embrace-whit... #Python
πΊοΈ October 24, 2026 5/5 countries in 6/13 guesses π‘π’π’π’π’ whereabouts.earth/daily/ #Whereabouts
βI find that I very rarely need the random function, because I often need random integers, not random floating point numbers.β Read more π pym.dev/random-numbe... #Python
When making a decorator, I highly recommend using wraps to help your replacement function pretend that it's actually the function you're decorating. For more on decorators see my "all about decorators" page, which links to screencasts and talks, as well as my course on making and using decorators.
πΊοΈ October 23, 2026 5/5 countries in 8/13 guesses π’π’π‘π π’ whereabouts.earth/daily/ #Whereabouts
βAnother reason I tend to avoid implicit string concatenation is that it can sometimes cause bugs by accidentally using it.β Read more π pym.dev/implicit-str... #Python
We tend to prefer shallow copies in Python: list(items), dict(mapping), slicing, and comprehensions all make new containers without copying the CONTENTS of those containers (tip #223's list(...) call is one example).
Within the standard library, deepcopy is used in about 8 files. list(...) is used in over 100. If you don't mutate objects that don't belong to you, you'll rarely need deepcopy. I'm not saying "never use deepcopy" (it certainly CAN be useful), just that every deepcopy call should be questioned.
πΊοΈ October 22, 2026 5/5 countries in 6/13 guesses π’π’π‘π’π’ whereabouts.earth/daily/ #Whereabouts
Passing a string to sys.exit prints it to standard error and exits with an error code (see tip #122). $ python3 weatherβ.py nope.json Error: No forecast file found at nope.json
def load_forecast(path): try: with open(path) as file: return json.load(file) except FileNotFoundError as error: raise WeatherError(f"No forecast file found at {path}") from error That "from error" keeps the original exception attached (see tip #143).
This was definitely the most islandful daily ever ποΈ Whereabouts Daily πΊοΈ October 20, 2026 5/5 countries in 5/13 guesses π’π’π’π’π’ whereabouts.earth/daily/ #Whereabouts
βThis will allow us to close the file and then pass the temporary file's filename to wherever it needs to be, and then when the context manager exits, the file will be deleted automatically:β Read more π pym.dev/temporary-fi... #Python