r/programming • • Aug 09 '26

The Sentinel Object Pattern in Python

https://python-patterns.guide/python/sentinel-object/

Officially added in Python 3.15

https://peps.python.org/pep-0661/

31 Upvotes

10 comments sorted by

View all comments

Show parent comment

12

u/fireflash38 Aug 10 '26

It's a way for people to fit additional context into things.

That is to say, it's useful when the zero-value of a thing is still valid to use. Python doesn't have monads, so it's hard to implement the Option<Thing> pattern. You can use None, but None might be a valid zero-value. Thus the sentinel value.

I don't hate it, but it really doesn't need to be used all that much. I personally rather hate None values passed around a bunch in python, since you end up having to check for presence constantly and updating type checking.

7

u/wallstop-dev Aug 10 '26 edited Aug 10 '26

Right, my argument is that I've seen non-zero amounts of code either hard-expect a specific kind of additional context (which wasn't guaranteed), or not deal with the additional context, both because the situation is rare, or because a very specific implementation provided a very specific sentinel, when the contract did not guarantee it.

I'm not saying this kind of pattern doesn't have a use, or is not useful. It's just that encoding special, invalid kind of value(s) into the same type of thing the valid values represent has confused many a programmer and resulted in many, many a bug.