r/Python • u/Ninteendo19d0 • 2d ago
News PEP 824: None-coalescing operators is approved
The Steering Council has decided to accept PEP 824 (on the condition that the ??= operator is removed, leaving only the ?? operator in the proposal).
Old: lst = [] if lst is None else lst
New: lst = lst ?? []
Discussion thread: https://discuss.python.org/t/pep-824-none-coalescing-operators/109161/93
The decision on PEP 823 – None-aware access operators is still pending.
339
Upvotes
106
u/skjall 2d ago
The none-aware access operators is the big one, really hoping it gets in soon. This is nice too of course!
76
u/aes110 2d ago
Nice! With question marks being used for null purposes, I hope this opens the way for other syntax like
foo?.barinstead offoo.bar if foo else Noneor
def foo(x: int?)instead ofdef foo(x: int | None)29
u/Sin-nie 2d ago
Am I missing something but can't you already do this with 'or'?
40
u/Ran4 2d ago
The same year most stopped writing python by hand :(
16
u/TPECKA180 2d ago
I like this addition, especially because
orisn't always a safe replacement for an explicit None check. Sometimes 0, False, or an empty list are perfectly valid values.The
??operator makes that distinction much clearer without adding unnecessary verbosity. I'm also curious to see what happens with PEP 823.7
u/tangerinelion 2d ago
Also available is
lst = lst or []The
??version and theorversion differ on what happens with falsey-but-not-None values;lst = falsefollowed bylst = lst ?? []leaveslstasfalsewhilelst = lst or []changes it to[].Having two short-hand versions that behave differently seems ungood.
7
u/Nooooope 2d ago
Good. My primary language has been Java, but I've had to branch into JavaScript and Ruby for work, and they made me realize that having operators specifically for null-handling is kind of a godsend.
33
u/rghthndsd 2d ago
Trading readability for fewer characters will always be a sad day for me.
2
u/TheBlackMafia 1d ago
Finally lol. The fact that
0 ?? 5stays 0 instead of becoming 5 is genuinely useful. Kinda curious why they dropped??=tho2
u/HovercraftOptimal971 1d ago
A none coalescing operator cleans up a common python pattern since chain of none checks shrink into one readable expression once the syntax lands. Bootdev could help keep python skills current as the language evolvee though the pep text itself also explains the reasoning.
9
u/MorrarNL 2d ago edited 2d ago
Why not:
lst = lst or []
Most often it is pretty much the same and shorter without the need for a new operator....
Sure things like False, 0 and "" will also trigger the or, but in practice that doesn't seem too much an issue if you want lst to be a list...
2
u/shiningmatcha 2d ago
such good news!
2
u/DeaD_EyE_1 2d ago
It's useful and shows the intention. Use the object on the right side, if the object on the left side is None.
1
u/Charming_Couple_6782 1d ago
How have I ever wrote any code for the last 15 years without this?🫣
It’s possible to tweak things too much - do we really need new operators for this. Feels like python is getting less pythonic every year
1
u/Internal_Pay4861 13h ago
The
oroperator watching everyone replace it with something more specific.1
u/Uncreativite 2d ago
fuckin finally
0
u/def__eq__ 2d ago
I am of a similar opinion, but on the other hand if something occurs so often, like “if … is None then …”, then it does help with readability…
0
u/denehoffman 2d ago
Love this, it’s about time
0
u/EternityForest 1d ago
This seems a little bit niche to be an operator, I'd much rather have Typescript's ?. And ?[] safe navigation operators, but it will probably be useful when you need prioritized layered configuration
-5
u/Grouchy-Friend4235 2d ago
Very unpythonic. The Steering Council is a bunch of spineless cheats who subcumb to anything proposed to them. It's hilarious.
-4
u/Short_Inspection_746 2d ago
Apparently the steering council no longer cares about readability and has no issues adding superfluous operator to the language. :(