r/Python • • Apr 28 '26
News PEP 661 (Sentinel Values) has been accepted for release in 3.15!
After five years of discussion, PEP 661, which adds support for sentinel values, has been accepted and is due for a release in 3.15. The use case is relatively simple:
MISSING = sentinel('MISSING')
class Logger:
def __init__(self, level:MISSING|None|str = MISSING):
if level is MISSING:
self.level = get_global_default_level()
else:
self.level = levelThere are 3 possible outcomes that can be handled by this pattern
- If no argument was provided, use the default
- If None is passed, disable logging
- If a level is passed, use it
The important thing here is a specific way to check if any argument was provided to the function, vs a caller propagating a None to it. The ability to check if an argument was actually provided by the caller was a great feature I liked in FORTRAN, so it's nice that it's made it to python!
316
Upvotes
233
u/Original-Ad-4606 Apr 28 '26
I’m not sure what all the hate is about. I for one have had many situations where None actually means something and have needed to create my own implementation of a Sentinel value to tell the difference between None and the user simply not providing a value.
Many libraries have had to implement Sentinels internally… Pandas and Pydantic come to mind.
I welcome this feature!
40
u/el_extrano Apr 28 '26
Pedantic point, but the PRESENT intrinsic to test whether an argument was supplied is only available in Fortran 90 and later, so you can't say it's from "FORTRAN": the all caps spelling is only for the 77 standard and earlier!
22
u/TMiguelT Apr 28 '26
Isn't your example incorrect? You have
level=MISSING|None|strbut I think you meanlevel: MISSING|None|str = MISSINGno? On my first reading I thought this was a bizarre change to how the|operator worked but I now realise that this PEP only addssentinel(which admittedly is handled specially by the type system).30
u/binaryfireball Apr 28 '26
i guess it's nice but tbh i dont feel like the benefits outweigh cluttering the language with even more features. One of the things I like about python is that its fairly idiomatic in the sense that there is one(or a couple) generally accepted way to do things. Pergaps I dont work in the same domain as you but the amount of times ive had to worry about this issue is negligible.
edit: im pretty sure you could achieve the same thing with a constant passed as default or at least in cases where theres not a ton of args
12
u/Marksta Apr 28 '26
Love it, I feel like Python has already been so wobbily on wtf None is way more than other languages are with their NULL/VOID etc types. None isn't actually nothing, it has meaning all over the place. Especially with the truthy-falsey stuff. Giving a formal way to discern the implicit None and an explicit None seems like a good move to me.
21
u/Uncle_DirtNap Pythoneer Apr 28 '26
This is GREAT NEWS, and haters have to get over it, this is super useful.
7
u/tobsecret Apr 28 '26
I like this. When it comes up, it's nice to have a prebuilt solution that has considered the common edge cases already.
19
u/teerre Apr 28 '26
Anything but imponent real enums lol
4
u/caprine_chris Apr 28 '26
This is amazing.
4
u/dusktreader Apr 28 '26
FINALLY.
7
u/mpersico Apr 28 '26
Named arguments and just don’t pass it. For a language that was supposed to be so simple the amount of stuff that’s being piled on makes it look like C++.
8
u/Orio_n Apr 28 '26
Whats wrong with an enum?
8
u/Trang0ul Apr 28 '26
Why not? If this is the new “official” pattern, the stdlib should lead by example. Otherwise it’s just another optional idiom, not a standard.
6
u/No_Flounder_1155 Apr 28 '26
I don't think the logging example is very good. Why should None stop logging from working. Seems really weird.
2
u/Dull-Researcher Apr 28 '26
Excellent for default immutable values or placeholders for the same, when None has a different meaning.
2
u/IcecreamLamp Apr 28 '26
I prefer the syntax of
unittest.mock.sentinelover this2
u/llun-ved Apr 29 '26
How is this appreciably different than:
sentinel = object()
def fn(myarg = sentinel): if myarg is sentinel: whatever.
I’ve used these for years without issue.
4
u/spinwizard69 Apr 28 '26
Things like this lead me to actually believe we need to bring back mental health prisons (Hospitals) and heavily applied ECT. What the hell are these people up to, trying to turn Python into C++? Pretty soon people will find themselves reading every program line in a Python program several times just to figure out what it is doing. idiomatic Python died today.
3
u/samettinho Apr 28 '26
Honestly, I've never needed this before and I can't really see the benefit; maybe something very minute.
2
u/jimanri Apr 28 '26
I dont get it. to me this just looks like and invisible if, since the None case is do nothing and yet is not explicit.
1
u/denehoffman Apr 28 '26
Another example:
Adding an argument like
no_timeout: bool’ is semantically confusing and allows for unused state in the signature (connect(timeout=30, no_timeout=True)`).Why can’t you just use
object()for the sentinel? Well for one, it doesn’t serialize, so if you wanted to store some user settings without storing the default (for example, you want to store the fact that the user didn’t set the value but allow the default to change in future versions), you’d have trouble. It also doesn’t play well with type hints, since it just has a type of object.1
u/OkTrack9724 Apr 30 '26
u/AskGrok please explain me that like I'm a HTML programmer
1
u/Honest-Estate-4592 May 01 '26
completely make sense, this is great, I will definitely help the code readability.
1
u/zachoiswacko May 05 '26
I actually like it, not sure why many of the comments online are negative.
1
u/UltraPoci Apr 28 '26
Can't we just use EllipsisType?
-10
u/BDube_Lensman Apr 28 '26
The number of cases where
MISSINGandNoneare able to have legitimate differences in semantic meaning has to be likes of cases, and we're added even more crap to the language to cover those. That's a thumbs down, people should not use this in their code.-13
u/FrickinLazerBeams Apr 28 '26
Oh cool look more junk. That's awesome.
-9
u/Effective-Total-2312 Apr 28 '26
I don't really understand the benefit. If you use type hints and static type checkers, why would you be doing this kind of thing ? Sounds like a solution to a problem that is only hiding a design issue behind
0
u/assumptionkrebs1990 Apr 28 '26
Nice solution (I would have likely build a provider (sentinel) wrapper class to handel such cases and then fallen into feature creep.)
0
u/RedSinned Apr 28 '26
Ironically I was at PyConDe 2 weeks ago and there was a talk about sentinel values. The talk is not online yet but the slides are: https://pretalx.com/pyconde-pydata-2026/talk/88TTRY/
70
u/[deleted] Apr 28 '26
[removed] — view removed comment