r/Python • • 49m 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.

40 Upvotes

9 comments sorted by

•

u/TPECKA180 36m ago

I like this addition, especially because or isn'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.

•

u/Sin-nie 45m ago

Am I missing something but can't you already do this with 'or'?

•

u/attoPascal 43m ago

or checks thuthiness, the new ?? operator checks only against None.

•

u/FI_Stickie_Boi 42m ago

Your value might be falsy, like [] or 0.

•

u/pacific_plywood 42m ago

“Or” operates on falsiness, this is specific to None

•

u/Temporary_Pie2733 30m ago

[] or 3, [] or 3, and None ?? 3 will all evaluate to 3. [] ?? 3 will evaluate to []. 

•

u/shiningmatcha 11m ago

such good news!

•

u/skjall 33m ago

The none-aware access operators is the big one, really hoping it gets in soon. This is nice too of course!

•

u/thisismyfavoritename 26m ago

opt-in rust like monadic optional would be even better