Sign in

Rodrigo Girão Serrão 🐍🚀

@mathspp.com
1.2K followers 313 following 1.3K posts

I'll help you take your Python skills to the next level! Python deep dive every Monday 🐍🚀 -> mathspp.com/insider Short daily drop of Python knowledge 🐍💧 -> mathspp.com/drops

PostsRepliesMedia
Rodrigo Girão Serrão 🐍🚀 @mathspp.com · 27/08/2026
Why does OOP exist? Why does it work the way it does? What's up with the magic in `self`? If you're new-ish to OOP I expect this article to fundamentally change the way you understand OOP. (For the better!) If it doesn't, I failed. You can read it here 👉 mathspp.com/blog/why-oop...
mathspp.com
Why OOP exists
Learn the fundamental principles behind OOP and how they connect to the syntax of Python.
010
Rodrigo Girão Serrão 🐍🚀 @mathspp.com · 26/05/2026
Python functions summarised in sentence: “Functions either return something or they blow up” — A student of mine
150
Rodrigo Girão Serrão 🐍🚀 @mathspp.com · 24/04/2026
Your frontier of competence is expanded by LLMs based on your current knowledge.
A diagram showing how LLMs are more useful if you know more a priori. Two circles of different sizes represent your current knowledge and the LLM extends either circle by the same amount, so if your initial circle is larger than your ending circle is also larger.
210
Rodrigo Girão Serrão 🐍🚀 @mathspp.com · 20/04/2026
I'm trying to reach out to Charlie Marsh @crmarsh.com because of the upcoming EuroPython conference. Do I know anyone here who can help me get to him? Thanks 🙏
002
Reposted by Rodrigo Girão Serrão 🐍🚀
Fredrik Mellström @traal.se · 09/04/2026
These uv Python skills for coding agents look promising. From @mathspp.com mathspp.com/blog/uv-skills
mathspp.com
uv skills for coding agents
This article shares two skills you can add to your coding agents so they use uv workflows.
121
Rodrigo Girão Serrão 🐍🚀 @mathspp.com · 08/04/2026
If you’re at PyCon Lithuania 🇱🇹 #PyConLT, join me to learn about the paradox of itertools.tee in the Diamond room at 11am. See you there!
022
Reposted by Rodrigo Girão Serrão 🐍🚀
Sam Rose @samwho.dev · 25/03/2026
I spent 2 months learning about quantization and am extremely proud of the post I've written about it. I think these are some of the nicest visuals I've ever made, and I love how this compression technique invented in 1898 is being used on the bleeding edge in 2026. ngrok.com/blog/quantiz...
ngrok.com
Quantization from the ground up | ngrok blog
A complete guide to what quantization is, how it works, and how it's used to compress large language models
1425155
Rodrigo Girão Serrão 🐍🚀 @mathspp.com · 23/03/2026
I forgot a semicolon and my site was down for ~4 hours without me even knowing. The joys of programming. 😂
031
Rodrigo Girão Serrão 🐍🚀 @mathspp.com · 23/03/2026
What's your favourite vanilla Python feature? (Abuses of the Python syntax are valid answers.)
100
Rodrigo Girão Serrão 🐍🚀 @mathspp.com · 19/03/2026
Astral just entered into an agreement with OpenAI to integrate their Codex team! openai.com/index/openai...
openai.com
OpenAI to acquire Astral
Accelerates Codex growth to power the next generation of Python developer tools
001
Rodrigo Girão Serrão 🐍🚀 @mathspp.com · 17/03/2026
Too many arrows?
Diagram using recursive structural pattern matching and unpacking inside comprehensions (new in Python 3.15) to write a function that flattens deeply nested lists of objects.
140
Rodrigo Girão Serrão 🐍🚀 @mathspp.com · 12/03/2026
Python decorators blueprint: a step by step explanation of the full decorator anatomy. Steal this for when you're writing your own decorators.
Diagram showing the full decorator anatomy and the 9 parts that make up a general decorator.
172
Rodrigo Girão Serrão 🐍🚀 @mathspp.com · 02/03/2026
I wrote 0 blog posts in February. Today I already wrote 2. Go figure. 🤷
020
Rodrigo Girão Serrão 🐍🚀 @mathspp.com · 02/03/2026
Came up with 4 random ways to do this. 2, I recommend you use them (although 1 doesn't work!) The other 2 are really cool — they use fixed points and `itertools.groupby` — but they'll be overkill 99.9% of times.
000
Rodrigo Girão Serrão 🐍🚀 @mathspp.com · 02/03/2026
How do you collapse runs of adjacent spaces in Python? That is, shorter runs of 2+ consecutive spaces so there's only 1 space there? I can't come up with a simple/straightforward way to do it with `str.join` and I'm surprised... What am I missing?
410
Reposted by Rodrigo Girão Serrão 🐍🚀
Anže @anze3db.pecar.me · 27/02/2026
Hey all! It's almost time for the next Python Lisbon Meetup! 🐍 On Thursday, pyctrl will be talking about exceptions. We'll cover the origin of exceptions, their usage in OOP, how best to manage them, and more. It's going to be exceptional! 💎
122
Rodrigo Girão Serrão 🐍🚀 @mathspp.com · 23/02/2026
How do you read `*args` in a function signature? PLEASE reply with how you'd read it, I'm trying to prove someone wrong 🤣 Reposts are appreciated :D
401
Reposted by Rodrigo Girão Serrão 🐍🚀
Sam Rose @samwho.dev · 19/02/2026
What do LLMs see? I wrote a lil' tool that extracts the attention matrices out of open models and creates this typing visual, with each token's opacity changing according to its average attention score as the prompt progresses. Dimmer words are considered less important to the model.
1726444
Rodrigo Girão Serrão 🐍🚀 @mathspp.com · 19/02/2026
`itertools.compress` explained in a simple diagram. Useful when you need to filter an iterable of data based on a second iterable of flags or selectors.
Diagram showing how `itertools.compress` works, taking an iterable with data and an iterable with selectors and making a selection based on the Truthy/Falsy value of the selectors.
010
Rodrigo Girão Serrão 🐍🚀 @mathspp.com · 18/02/2026
How does this slide look?
Diagram explaining how starmap works across a stream of data for a given iterable with pairs (a, b) and a binary function f, producing the iterable with items f(a0, b0), f(a1, b1), etc.
000
Rodrigo Girão Serrão 🐍🚀 @mathspp.com · 12/02/2026
FINALLY had technical issues while delivering a remote talk. Today I got all of it! First, my computer froze and I couldn't even share my screen. Then, at some point my internet connection went POOF and I dropped off the call... 🤦 Hopefully now I'm good again for a dozen more talks.
220
Rodrigo Girão Serrão 🐍🚀 @mathspp.com · 12/02/2026
Catch you there?
010
Reposted by Rodrigo Girão Serrão 🐍🚀
Savannah Ostrowski @savannah.dev · 11/02/2026
Stoked to share that the Steering Council has accepted PEP 814. frozendict is coming to Python 3.15! discuss.python.org/t/pep-814-ad...
discuss.python.org
PEP 814: Add frozendict built-in type
After careful deliberation, the Python Steering Council is pleased to accept PEP 814 – Add frozendict built-in type. The absence of an immutable dict counterpart has been a long-standing gap in Pytho...
05713
Reposted by Rodrigo Girão Serrão 🐍🚀
Python Software Foundation @python.org · 10/02/2026
Introducing the PSF Community Partner Program! The PSF is very excited to announce this new in-kind offering that expands how we support Python community events and initiatives around the world, now and alongside future community support programs 💝🐍
pyfound.blogspot.com
Introducing the PSF Community Partner Program
The Python Software Foundation (PSF) is excited to announce the introduction of the PSF Community Partner Program. This new program is designed as an “in-kind” way for us to support Python events and initiatives with non-financial assistance through the use of the PSF logo and name, as well as promotional support via sharing qualified posts on PSF official social media accounts. The PSF looks forward to supporting Python community events and initiatives through this new program!The introduction of the PSF Community Partner Program grew out of our desire to find alternative ways to support the community during the pause of our Grants Program (read more about the resulting process below). Even so, we intend to continue offering this in-kind support program after the Grants Program reopens. Our big picture hope is that, over the long term, some community events and initiatives will continue to partner with the PSF while being financially dependent on sponsors and individual donors alone. The PSF is also working on the future of our Grants Program, including when and how we can reopen it in a way that ensures the program’s long-term sustainability while balancing the needs of the Python community. In light of the truly staggering outpouring of support from our community during the 2025 year-end fundraiser, we are now in a stronger position to reopen the Grants Program and are eager to give back in a thoughtful and sustainable way. More updates to come!As with the rollout of any new program, we anticipate small adjustments will need to be made for processes to flow smoothly and to ensure the program serves the Python community well. The PSF welcomes your comments, feedback, and suggestions regarding the new Community Partner Program on the corresponding Discuss thread. We also invite you to join our upcoming PSF Board or Grants Program Office Hour sessions to talk with the PSF Board and Staff synchronously. If you wish to send your feedback privately, please email grants@python.org. 
1109
Rodrigo Girão Serrão 🐍🚀 @mathspp.com · 09/02/2026
This list is missing the soft keywords. How many are there, and what are they?
110
Rodrigo Girão Serrão 🐍🚀 @mathspp.com · 09/02/2026
I've written this Python function so many times I should no longer be confused by it... But I never know if it's def clamp(v): return min(max(v, low), high) or def clamp(v): return min(max(v, high), low) Always need to stop and think about it for a minute.
020
Rodrigo Girão Serrão 🐍🚀 @mathspp.com · 03/02/2026
If they had used the built-in `range` this wouldn’t happen.
Photo of a set of buttons inside an elevator. From the bottom up, the numbers go 2, 0, 4, 5, 6, …
250
Rodrigo Girão Serrão 🐍🚀 @mathspp.com · 02/02/2026
It's pretty cool how much you can get done in Python with so little code if you leverage the standard library.
Python code snippet with the following code:

from collections import deque
from itertools import islice, tee

def nwise(iterable, n):
    iterators = tee(iterable, n)
    for offset, it in enumerate(iterators):
        deque(islice(it, offset), maxlen=0)
    yield from zip(*iterators)
010
Rodrigo Girão Serrão 🐍🚀 @mathspp.com · 30/01/2026
One of my students today made this mistake. If you have a project and want to test it, your tests will import your project. So, you need to install pytest as a dependency INSIDE your project. If you install it globally, when you run it, it can't see your own project.
Diagram showing how using uv to install pytest globally will isolate it from everything else, as it should. But if you're testing a package, you want to have pytest as a dependency of that package since it'll be able to find your own package code.
200
Rodrigo Girão Serrão 🐍🚀 @mathspp.com · 30/01/2026
Dump JSON and make it Human-readable.
Diagram showing how using the parameters `indent` and `sort_keys` can make the output of dumping JSON much better in Python.
010
Rodrigo Girão Serrão 🐍🚀 @mathspp.com · 29/01/2026
Parse JSON from a string with a single function call. Don't use this if you have JSON in a file. Use `load` for that. This is for when the data comes in a payload, for example.
Simple diagram showing how to use `json.loads` to parse a native Python object from a JSON string.
000
Rodrigo Girão Serrão 🐍🚀 @mathspp.com · 28/01/2026
When writing CLIs and scripts for the terminal, use `sys.exit` to set the exit code of your program. ✅ An exit code of 0 means success; your program terminated without any issues. ❌ Any other integer exit code means “error”.
Diagram showing how to use `sys.exit` to set the exit code of a program and how to use the command `echo $?` to check the exit code of the last command in the terminal.
110
Reposted by Rodrigo Girão Serrão 🐍🚀
EuroPython 2026, Kraków @europython.eu · 27/01/2026
“Nothing beats the smiles and thank you’s you get when the conference is over. Plus, it is an amazing feeling to be part of something bigger than yourself.” Check out the interview with @mathspp.com, Programme Team for EuroPython 2025 blog.europython.eu/humans-of-ep... #europython #conference
012
Rodrigo Girão Serrão 🐍🚀 @mathspp.com · 27/01/2026
Day 2 of my intermediate Python course: I didn't forget to start the recording. That's a great start to the day :D
030
Rodrigo Girão Serrão 🐍🚀 @mathspp.com · 27/01/2026
Before being added in Python 3.10, itertools.pairwise was commonly implemented in terms of itertools.tee. Isn't this elegant?
Diagram showing how to implement pairwise in terms of tee.

Code:
def my_pairwise(it):
    it1, it2 = tee(it, 2)
    next(it2)
    yield from zip(it1, it2)
121
Rodrigo Girão Serrão 🐍🚀 @mathspp.com · 26/01/2026
`typing.reveal_type` explained: Runtime vs static type checking time.
The module `typing` provides a debugging aid, the function `reveal_type`.
When a static type checker encounters it, the static type checker emits a note with the _inferred_ type of the expression passed into `reveal_type`:

```py
def s() -> str:
    return 3

reveal_type(s())
# Revealed type is "builtins.str"
```

The function `reveal_type` doesn't even need to be imported and the static type checker infers that `s()` is a string because of the return type `-> str`.
(The static type checker also complains about the fact that we're returning an integer from inside `s` while the function should return a string, but you'll see why I'm doing that in a second.)

At runtime, the function `reveal_type` prints the runtime type of the argument to `sys.stderr` and returns the argument unchanged:

```py
from typing import reveal_type

def s() -> str:
    return 3

reveal_type(s())
# Runtime type is 'int'
```

At runtime, the type of `s()` is `int` because the function `s` returns the integer 3.
000
Rodrigo Girão Serrão 🐍🚀 @mathspp.com · 25/01/2026
Python typing pop quiz. Take a look at the function `choice` shown below. Note how it's being called as `choice(3, "hey!")`. Will this type check or not? If it does, what's the revealed type? The right answer might surprise you! It surprised me. 🤷
Code listing with the following Python code:

from typing import reveal_type
import random

def choice[T](left: T, right: T) -> T:
  if random.random() < 0.5:
    return left
  else:
    return right

reveal_type(choice(3, "hey!"))
110
Rodrigo Girão Serrão 🐍🚀 @mathspp.com · 24/01/2026
This is how you learn to write really good code. In Python, and in any other programming language 👇
111
Rodrigo Girão Serrão 🐍🚀 @mathspp.com · 23/01/2026
Has anyone here, in the data science world, successfully moved from using conda/miniconda/... to using uv?
132
Rodrigo Girão Serrão 🐍🚀 @mathspp.com · 23/01/2026
Python has a flexible mechanism built into `pathlib` to look for files with filenames that follow a given pattern. You can use `[...-...]` to represent character ranges and `*` to match arbitrary text. But note that `glob` does NOT support regex syntax! This is just similar.
Diagram showing how character ranges and the asterisk can be used in `pathlib.Path.glob` to match multiple filenames that follow the same pattern.
121
Rodrigo Girão Serrão 🐍🚀 @mathspp.com · 21/01/2026
These two typing features look similar... But they're very different. Here's how to use NewType and TypeAlias in Python 👇
Diagram with code snippets showing how to use TypeAlias and NewType. The content of the post walks through the diagram.
111
Rodrigo Girão Serrão 🐍🚀 @mathspp.com · 21/01/2026
Pandas 3.0.0 is out now!
Screenshot of the pandas 3.0.0 release on GitHub.
011
Rodrigo Girão Serrão 🐍🚀 @mathspp.com · 21/01/2026
In hindsight, if you're seeing this on your phone, the QR code isn't very helpful... Just go to mathspp . com / drops 🤣
000
Rodrigo Girão Serrão 🐍🚀 @mathspp.com · 21/01/2026
Just finished scheduling 3 tips for today, tomorrow and Friday. If you want to get smarter about Python in just 3 min/day, follow the QR code to sign-up.
Screenshot of my newsletter scheduled posts showing three upcoming emails: typealias vs newtype, character ranges in glob search, and expanding regex matches. The image also includes a QR code that takes you to mathspp.com/drops where you can sign-up to get these tips.
010
Rodrigo Girão Serrão 🐍🚀 @mathspp.com · 20/01/2026
Convert data to a JSON string without writing to a file: Just use `json.dumps`! (The final S stands for String!) ```py import json data = {"k1": True, "k2": [73, 42, 10]} s = json.dumps(data) print(type(s), s) # <class 'str'> {"k1": true, "k2": [73, 42, 10]} ```
Diagram showing how to use `json.dumps` to convert data into a JSON string.

Code snippet:

```py
import json

data = {"key1": True, "key2": [73, 42, 10]}

dumped = json.dumps(data)
print(type(dumped), dumped)
# str {"key1": true, "key2": [73, 42, 10]}
```
110
Rodrigo Girão Serrão 🐍🚀 @mathspp.com · 19/01/2026
Let me teach you to break a Python law. Iterators can only be traversed once: ```py squares = (x ** 2 for x in range(3)) for sq in squares: print(sq, end=", ") # 0, 1, 4, for sq in squares: print(sq, end=", ") # <no output> ``` The second loop produces no output!
Diagram showing how to use `itertools.tee` to split an iterator. The code snippets of the diagram are the ones in the post.
140
Rodrigo Girão Serrão 🐍🚀 @mathspp.com · 18/01/2026
`itertools.pairwise` takes an iterable and produces overlapping pairs of consecutive elements: ```py from itertools import pairwise queue = ["John", "Joe", "Ana"] for f, b in pairwise(queue): print(f"{f}'s ahead of {b}.") # John's ahead of Joe. # Joe's ahead of Ana. ```
Diagram showing how `itertools.pairwise` works.

Code snippet:

```py
from itertools import pairwise

queue = ["Harry", "Hermione", "Ron"]

for front, back in pairwise(queue):
    print(f"{front} is directly in front of {back}.")
```

Output:

```text
Harry is directly in front of Hermione.
Hermione is directly in front of Ron.
```
110
Rodrigo Girão Serrão 🐍🚀 @mathspp.com · 17/01/2026
This uv cheatsheet lists 40+ of the most and useful uv commands. It groups the commands into 9 sections that correspond to major features/capabilities that uv has. The cheatsheet is free to download and comes in light mode, dark mode, and high-contrast mode. mathspp.gumroad.com/l/cheatsheet...
uv cheatsheet with 40+ commands and variations split into 9 categories.
043
Rodrigo Girão Serrão 🐍🚀 @mathspp.com · 16/01/2026
A regex match has the method `groups`. This method returns a tuple with all of the groups that the pattern contained. Here's an example 👇
Diagram showing how to use `re.Match.groups` with a default value. The code snippets in the diagram match the ones shared in the post(s).
200
Rodrigo Girão Serrão 🐍🚀 @mathspp.com · 15/01/2026
If your code type checks and you don't have `Any` or `# type: ignore` then you won't get type errors at runtime. Right? Wrong... ```py x: object = [] hash(x) ``` This code type checks but raises a `TypeError` at runtime... What's going on here?
300