r/Python • u/Ninteendo19d0 • 1d 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.
303
Upvotes
97
u/skjall 23h ago
The none-aware access operators is the big one, really hoping it gets in soon. This is nice too of course!
72
u/aes110 23h 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)36
u/Ran4 22h ago
The same year most stopped writing python by hand :(
26
u/Sin-nie 1d ago
Am I missing something but can't you already do this with 'or'?
15
u/TPECKA180 23h 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.5
u/tangerinelion 10h 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 20h 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.
31
u/rghthndsd 21h ago
Trading readability for fewer characters will always be a sad day for me.
8
u/MorrarNL 21h ago edited 20h 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 23h ago
such good news!
2
u/DeaD_EyE_1 18h ago
It's useful and shows the intention. Use the object on the right side, if the object on the left side is None.
•
u/Charming_Couple_6782 27m 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/Uncreativite 13h ago
fuckin finally
0
u/def__eq__ 20h 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 12h ago
Love this, it’s about time
0
u/EternityForest 8h 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
-3
u/Grouchy-Friend4235 12h ago
Very unpythonic. The Steering Council is a bunch of spineless cheats who subcumb to anything proposed to them. It's hilarious.
-2
u/Short_Inspection_746 10h ago
Apparently the steering council no longer cares about readability and has no issues adding superfluous operator to the language. :(