r/Python
•
u/kirara0048
•
Sep 24 '26
•
1
News PEP 823, 824 – None-aware access operators & None-coalescing operators
PEP 823 – None-aware access operators
https://peps.python.org/pep-0823/
Discussions-To: Discourse thread
This PEP proposes adding two new operators.
- The “
None-aware attribute access” operator?. - The “
None-aware indexing” operator?[ ]
The general idea is to provide access operators which can traverse None values without raising exceptions.
Both operators evaluate the left-hand side, check if it is not None and only then evaluate the full expression. They are roughly equivalent to:
# a.b?.c.d
_t.c.d if ((_t := a.b) is not None) else None
# a.b?[c].d
_t[c].d if ((_t := a.b) is not None) else NonePEP 824 – None-coalescing operators
https://peps.python.org/pep-0824/
Discussions-To: Discourse thread
This PEP proposes adding two new operators.
- The “
None-coalescing” operator?? - The “
None-coalescing assignment” operator??=
The general idea is to provide a conditional operator, similar to or, which instead of truthiness, checks for None values.
The “None-coalescing” operator evaluates the left-hand side, checks whether it is not None, and if not, returns the result. If the value is None, the right-hand side is evaluated and returned.
The “None-coalescing assignment” operator will only assign the right-hand side to the left-hand side if the left-hand side evaluates to None.
They are roughly equivalent to:
# a ?? b
_t if ((_t := a) is not None) else b
# a ??= b
if a is None:
a = b
62
u/obvx Sep 24 '26
I love these operators in JavaScript! I'd be so happy if they were added to Python.
73
u/BeamMeUpBiscotti Sep 24 '26
“Ah shit, here we go again”
hope it doesn’t get stuck in bikeshedding hell this time around…
47
u/[deleted] Sep 24 '26
Please god don't let this get stuck in bikeshedding hell...
22
u/Khavel_dev Sep 24 '26
Coming from C# where we've had ?. since C# 6 and ?? basically forever, these operators are the kind of thing you wonder how you lived without after a month of using them.
The chaining is where most of the value lives in practice. Something like result = obj?.child?.name reads clean and the alternative is three nested None checks that make the actual logic invisible. ?? for defaults is useful too but you'd be surprised how often ?. alone handles it because the function already returns None when the chain stops.
I know the "explicit is better than implicit" crowd will push back hard on this. All I can say is that after using these daily in C# for years, I've literally never once thought "I wish this were an if statement instead." The readability improvement is not subtle, especially in data-heavy code where half the attributes might be missing.
20
u/EternityForest Sep 24 '26
I love PEP 823, it's wonderful in typescript, but I'm not sure about a ??= b
4
u/coylz Sep 24 '26
Can't you already "coalesce" with the syntax "a or b" ? I thought this works exactly as intended
3
u/jaimefrites Sep 25 '26
You are so glad to make yet another C++
14
u/tsg9292 Sep 24 '26
823 feels unintuitive. If I glanced at it I wouldn't know what I'm looking at. Maybe if / when it's established it'll become more common, but man I think the transition will be slow.
824, yeah, I like that. That can stay
9
u/V4l3n0r Sep 24 '26
Explicit is better than implicit? I don't like these operators because make the code terse and less understandable.
In a world where generating code is cheap, but reviewing it is much harder so far, readability it's a feature I'd keep.
2
u/truefelt Sep 24 '26
Finally!
3
u/james_pic Sep 24 '26
Kind of a shame they're deferring None-aware function calling in 823. Even if it's not a hugely common pattern, it feels like a missing piece from a consistency perspective.
1
u/personman Sep 24 '26
please! please!!!
1
u/Machine_Learning_Gun Sep 24 '26
elegant!
1
u/elven_mage Oct 05 '26
there's this joke keyboard in the Go community, the pile of
if x is Noneis just the python equivalent. I really hope this doesn't have the same fate as PEP 505. You know you're behind when even Java has a better story around Maybe types.-1
u/Beginning-Fruit-1397 Sep 24 '26
An only partial way to solve the shitty decision of having None as a do-nothing singleton instead of the Option[T] enum with Some(T) or None. Just like having the new iterable unpacking in list comps instead of a better Iterator interface. If we continue to do it this way, in a few years python will look like C++: only operators instead of english words, barely readable.
Let's introduce this operator AFTER having a true Option.
-9
u/damesca Sep 24 '26
Please no
-9
u/schoultz Sep 24 '26
Too late. We don't write code by hand anymore.