concurrent.futures now includes an InterpreterPoolExecutor, which spawns a new Python interpreter in a separate thread, but in the same process
Read more 👉 https://trey.io/flfwr9
#Python
Latest Posts by Trey Hunner
Python Tip #97 (of 365):
When you need to read/write a whole file, use a pathlib.Path helper method.
Instead of:
with open(path) as f:
text = f.read()
Do:
text = path.read_text()
And instead of:
with open(path, "w") as f:
f.write(text)
Do:
path.write_text(text)
#Python #DailyPythonTip
If you need a os/os.path/glob to pathlib conversion cheat sheet, see pym.dev/pathlib-modu...
If you just want help adopting pathlib in general, use this cheat sheet instead:
pym.dev/pathlib-modu...
This week's tips are all about pathlib patterns & anti-patterns.
Not convinced that pathlib is better than os.path and friends?
Well, unlike with strings:
1. It's hard to get yourself into trouble with pathlib.Path
2. When I see a pathlib.Path object, I know we're representing a path
I have a whole talk that might help convince you: trey.io/pathlib-talk
Python Tip #96 (of 365):
When manipulating file paths or asking questions about them, use pathlib 🧵
"Stringly typed code" uses a string to represent a value when a better type exists.
For representing file paths, pathlib.Path objects are that better-than-a-string type.
#Python #DailyPythonTip
Did you know that you can add aliases to PDB with a ~/.pdbrc file?
I made one over the weekend with some very helpful aliases.
I can now use these to inspect an object (note that "dir" and "vars" are like the dir() & vars() functions but better):
attrs x
dir x
vars x
Also these:
src func
loc
Just got my ticket to #PyConUS. I’m excited.
Last year was my first PyConUS. I hardly went to any talks; I mostly spent my time in the hallway. For me, a conference is about connecting with people. Being there in person is pure gold.
All escape sequences start with a backslash (\) in Python.
Read more 👉 https://trey.io/ca0qyn
#Python
Looking through one of the four windows of the Orion spacecraft, a tiny crescent Earth is illuminated against the blackness of space and grows smaller as the crew journeys closer to the Moon. Part of the window edge is visible; the rest is darkness. [Alt-text slightly revised from ESA/NASA version]
We are so, so small. #Artemis
I use NASA's pale blue dot revisited as the background on all my devices to help remind me of this frequently.
The scale of our universe and of time is so much bigger than we can comfortably fathom.
“Even if you don't use them directly, dataclasses are a good measuring stick for your class-based code.”
Read more 👉 pym.dev/friendly-cla...
#Python
re.match() is a weird middle ground between re.search() and re.fullmatch().
re.fullmatch() didn't used to exist but re.match() did... which is why fullmatch isn't called "match". This is all confusing due an accident of history.
Personally, I usually just stick with re.search().
Using re.fullmatch() is the same as using re.search() with ^ and $ used to anchor to the beginning and end of the string.
In other words, these are equivalent:
match = re.search(r"^\w+$", string)
match = re.fullmatch(r"\w+", string)
There are 2 main things we use regex matching for.
1. Finding a regex within a string
2. Matching a regex against a whole string
re.match() does neither of those.
To find a match WITHIN a string, you can use re.search().
To match an entire string against a regex, you can use re.fullmatch().
Python Tip #95 (of 365):
Don't use re.match(): it's confusing 🧵
I'm not sure I've ever seen re.match() used when it wasn't being used by mistake.
If you think you want re.match(), you probably want either re.search() or re.fullmatch() instead.
#Python #DailyPythonTip
This is something that I've wanted for a very long time. Very excited @savannah.dev made this!
Go give @coredispatch.xyz a follow to keep up with Python!
See you there!
See you there friend!
You going to the WebAssembly summit? I'd love to pick your brain about Python-in-the-browser stuff at some point.
I'm getting an early start on my personal "list of folks on bsky who will be at @pycon.us this year".
If you'll be at #PyConUS and want to run into each other, 👋 reply here (or like or something).
If you'll be on Mastodon during PyCon, respond there also/instead: mastodon.social/@treyhunner/...
“Python's if statements don't use parentheses.”
Read more 👉 pym.dev/unnecessary-...
#Python
That inner group isn't for capturing... it's just for grouping the alternatives with that | operator.
To make it non-capturing we can put ?: just after the opening paren:
>>> re.findall(r"`((?:\\`|[^`])+)`", text)
['[', ']', '\\`']
Find that hard to read?
I agree.
Remember re.VERBOSE (tip 92)!
We ended up with a list of 2-item tuples.
What's happening is that the re.findall() function only returns strings when there's either no capture group or 1 capture group... but when there are 2 or more capture groups, it returns a tuple of those group matches.
We need a non-capturing group.
We could use a group with an alternative, looking for a \ followed by a backtick OR any non-backtick character: (\\`|[^`])
We could quantify this with + to match it one or more times.
But this doesn't work:
>>> re.findall(r"`((\\`|[^`])+?)`", text)
[('(', '('), (')', ')'), ('\\`', '\\`')]
Python Tip #94 (of 365):
Improve your use of re.findall() with non-capturing groups 🧵
We're modifying our backtick searching regex (tip 93) to work with escaped backticks within backticks, to look for inline code like this:
>>> text = "`(`, `)`, and `\\`` are special."
#Python #DailyPythonTip
Here's a bunch of tips I wish I'd learned before my first one: trey.io/first-pycon
If you can, I'd recommend attending the newcomers orientation on Thursday night.
See you there!
If you're new to your Python journey, I recommend this.
PyCon US is big (~2,000 attendees) which might feel intimidating but it's very likely the friendliest tech conference of its size.
If you attend, come to the newcomers orientation on Thursday if you can.
See you there!
PyCon US 2026 is May 13th to 19th in Long Beach, California. If you've been meaning to learn to program, I'm doing a 3-hour tutorial (additional registration required) for absolute beginners who have never coded before. I hope to see you there! us.pycon.org/2026/
I did not realize I could purchase a PyLadies shirt through my registration ticket this year until I saw this post. It may not have been an option when I signed up or I may have missed it, but it's there now!
Check the dashboard page and add your shirt(s)! 👕👕
us.pycon.org/2026/account...
Note that you COULD argue that the above regex should actually match non-backticks:
>>> re.findall(r"`([^`]*)`", text)
['any()', 'all()']
I can't argue with that.
Reaching for a non-greedy quantifier IS often a sign that there might be a more accurate way to write your regex... but not always.