Much Ado About None: Sentinel Values in Python
sebg
78 points
51 comments
October 29, 2026
Related Discussions
Found 5 related stories in 42.6ms across 3,874 title embeddings via pgvector HNSW
- High Stakes Are Strongly Typed supo · 18 pts · November 19, 2026 · 40% similar
- Arete-27B: Qwen3.8-27B, post-trained to reinforce Western values pretext · 14 pts · November 07, 2026 · 39% similar
- Eval Awareness vs. Eval Paranoia ezrasu · 64 pts · November 12, 2026 · 39% similar
- Ask HN: Why no AI-led memory vulns in Python? uticus · 11 pts · November 19, 2026 · 37% similar
- The Myth of Neutral Code birdculture · 26 pts · November 14, 2026 · 37% similar
Discussion Highlights (9 comments)
chowells
I like it. I can't see how to do better while remaining compatible with Python idioms.
RantyDave
"A simple cache returns None both for a missing key and for a key that maps to None." No, it returns KeyError. Or at least it should.
lpghatguy
When I was writing a bunch of Lua, I was a big fan of having a None sentinel value (Lua uses 'nil' for representing absence) and it was useful for a lot of things similar to Pydantic's MISSING. This distinction is something that I think is actually handy about JavaScript, and distinguishing between 'null' and 'undefined' is sometimes useful for similar reasons.
pwdisswordfishq
So much trouble to avoid introducing the optional monad.
throwaway2037
Here is the sample code from the article: > MISSING = sentinel("MISSING") > def update_email(user_id: int, email: str | None | MISSING = MISSING) -> None: > if email is MISSING: > return # nothing to update > elif email is None: > db.delete_email(user_id) > else: > db.set_email(user_id, email.lower()) # narrowed to str I see this style frequently from long time Python programmers. The language is so dynamic that people begin to think this is a good way to program. Hot take: It is not. Why not two different functions called delete_email() and set_email()? That would be more clear.
est
I just return a customized Exception type. Works about the same.
jMyles
I have a library that's approximately half joking that I use for (sentinel and other) special value constants. If you like reading test suites, this one will get ya: https://github.com/nucypher/constantSorrow/blob/master/tests...
shepherdjerred
I hate sentinel values. Even/especially with AI-generated code. 1. If your date does not have a value, do not return 0, an empty string, or None. Use a discriminated union and model your domain rigorously. 2. If your data model accepts None/undefined as a value, that should translate to your business model as well. For example 3. Do not make your `name` field as `string | undefined` just because there might be an intermediate loading or processing state. It's better to model that as something like `Loaded<string>` where Loaded is something like a Result or Optional type (though ideally more rigorously defined) Here is what I've done for years in TypeScript, though now in a nice package thanks for AI: https://github.com/shepherdjerred/monorepo/blob/main/package...
DarkNova6
Ah Python. The leaking sieve that keeps on giving problems to solve.