Python has been without a ?. and a ?? for the whole of its life, and every few years someone proposes them and the argument runs out of steam. This time looks different. Two draft PEPs, written by Marc Mueller and sponsored by Guido van Rossum, target Python 3.16, and the discussion threads have been busy since September. My position after reading both: I want ?? badly, and I'd use ?. about once a week.

The first proposal, PEP 823, adds the None-aware access operators ?. and ?[ ]. The second, PEP 824, adds the None-coalescing operators ?? and ??=. Both split up the older PEP 505, which has been deferred for years, and both say they can be adopted independently of each other. Real Python's November news roundup describes the pair as a push to bring ?. and ?? to Python more than ten years after the last attempt stalled. Both are drafts, and nothing here is accepted.

What the two PEPs actually propose

The coalescing operator is the simpler of the two. a ?? b evaluates a, and if the result isn't None it returns it, otherwise it evaluates and returns b. The PEP gives the rough equivalent as _t if ((_t := a) is not None) else b. The assignment form a ??= b assigns only when a is None. Chains like a ?? b ?? c go left to right until something isn't None, and the operator sits below or and above conditional expressions in precedence, so x or y ?? 2 parses as (x or y) ?? 2.

Side-by-side Python code comparing a nested is-not-None check with a single None-aware attribute chain from PEP 823

Expand

Side-by-side Python code comparing a nested is-not-None check with a single None-aware attribute chain from PEP 823

The access operators are the ones that draw the arguments. a?.b yields None if a is None, and otherwise a.b. a?[b] does the same for indexing. The PEP's own example turns a function full of nested is not None checks into return data.customer?.user?.name.lower(). It points out that the idea isn't new: TypeScript, JavaScript, C#, Dart, Swift, Kotlin, Ruby and PHP all have versions of it.

Guido has been active in the threads. On October 20 he posted a new version of PEP 823 with a mini-tutorial included under "How to Teach This", and the PEP's post-history shows a revision on November 1. That suggests the sponsor sees teaching cost as the real obstacle, and I agree.

Why the coalescing operator is the easy yes

Python already has a coalescing operator. It's or, and it's wrong whenever the value can legitimately be falsy. timeout = config.timeout or 30 replaces a timeout of 0 with 30. name = user.name or "unknown" replaces an empty string. items = arg or [] quietly throws away a list the caller deliberately passed in empty, if it was falsy for any reason.

The usual fix is the longhand x if x is not None else default, which repeats the expression and invites side effects if the expression is more than a name. PEP 824's examples show what the alternative looks like:

CodeCopy 

age = user.age ?? "unknown"
user.name ??= "unknown"
user ??= create_default_user()

Python code showing today's is-None checks beside the same logic written with the proposed coalescing operators from PEP 824

Expand

Python code showing today's is-None checks beside the same logic written with the proposed coalescing operators from PEP 824

The last line is the one I'd miss most. Function defaults in Python are evaluated once, in the parent scope, so any default that is mutable or depends on context ends up as None plus a check at the top of the function. ??= makes that check one line, and it says what it means: replace this only if it's None.

There's a small trap, and the PEP is honest about it. ?? tests for None only, so it won't turn an empty string into a default. That's the point, and it's also the reason you can't mechanically replace every or with ??. Some of your existing or expressions rely on falsiness on purpose.

Where ?. gets riskier

I'm less settled on the access operators, mostly because of what they hide. PEP 823 lists the caveats itself. The operator skips an access only when the left side is None. If the left side doesn't exist you still get a NameError. If it's not None and the attribute is absent you still get an AttributeError. a?[b] still raises KeyError or IndexError for a missing key or index, so dictionaries need a?.get(b). And a?.b.c still blows up when a.b is None, which is why people end up writing a?.b?.c.

The PEP's guidance on chains is the best argument against the worst version of the feature: you should rarely need more than one ?. or ?[ ] in a single chain, and if you find yourself writing a?.b?.c often, the schema could probably be improved, or a helper function would do. I'd go further. Every ?. converts "this raised an error where the None came from" into "None flows downstream and fails somewhere else". For parsing JSON-shaped data, where None really is an expected value, that trade is fine. For ordinary application objects, it's a way to turn a clear traceback into a mystery.

Assignment is another gap. a?.b = 42 and a?[b] = 42 are syntax errors, so the operators are read-only. a[b?.c] = 42 is allowed but may assign to a[None], which is a good example of the kind of surprise this syntax makes easy.

How the chain actually evaluates

The detail that decides whether ?. is safe is short-circuiting. In the PEP's abstract, a.b?.c.d is equivalent to _t.c.d if ((_t := a.b) is not None) else None. If a.b is None, the entire rest of the chain, .c.d, is skipped, and the next hop is only the start of it. That's how the PEP defines it, and it's why one ?. early in a chain protects everything after it.

It's also why readers get tripped. You can't tell by looking at a?.b.c.d which of .c and .d would have failed on a None, because none of them will run. The PEP's "When to use" section has the cleanest example. A function that checks if user is None: return None and then returns user.account shrinks to return user?.account. The same section warns that a truthiness check such as user.account if user else None may need review, since if user is not the same as if user is not None for objects that define their own truth value.

The objections are already printed in the PEP

PEP 823 has a "Common objections" section, and the headings tell you where the fight will be: that it's difficult to read, not Pythonic or not explicit enough, easy to get wrong, that short-circuiting is hard to understand, and that it encourages a proliferation of None in code bases. It also covers "None is not special enough" and "use custom sentinels instead". The PEP answers each. Whether the answers persuade is a different matter, and the long discuss.python.org threads suggest they persuade some people and not others.

My own objection is the proliferation one. If ?. makes None painless to pass around, more code will return None as a normal outcome. That's a design cost the syntax can't see. A language feature that makes a risky habit cheaper deserves some caution, even when each individual use is fine.

What I'd do in your code today

Nothing here ships soon. Python 3.15 only went final on October 9, delayed eight days by a third release candidate, and 3.16's second alpha is due on November 10. Even if the PEPs are accepted, a language feature that arrives in 3.16 can't be used in a library that still supports older versions. That includes most libraries.

You can still prepare. Search your code for or defaults on values that can legitimately be 0, empty or false:

CodeCopy 


shell
grep -rnE '= [A-Za-z_.]+ or ' src/

Review every hit and ask whether a valid falsy value could reach that line. If it can, you have a latent bug today, and you can fix it with an explicit is not None test. If you later move to a Python that has ??, those are the lines to convert first.

For the access side, the best preparation is schema discipline. Use dict.get where a key may be missing, keep Optional types honest in your annotations, and let type checkers tell you where .b can be None. PEP 823 notes that static checkers can flag a?.b.c when .b can be None, which is the kind of check that makes a None-aware chain safer than the longhand it replaces.

You can try the proposals without installing anything. The PEP links an online demo at pep823-and-pep824-demo.pages.dev, and a reference implementation as a CPython fork by the author. I haven't run either.

My take

Take PEP 824 first, and don't wait for the other. It fixes a problem every Python developer already has, in a form other languages have used for years, and the or-versus-?? precedence rule is sensible. The access operators are a closer call. I'd support them with the PEP's own limits written into style guides: one ?. per chain, and a rule that anything fancier gets a helper function or a better type.

The decision belongs to the steering council and the discussion on discuss.python.org. If you have a strong view, that's where it counts, and Guido's replies show that the thread is read.

Hero image: Alessio Bragadini via Wikimedia Commons, CC BY-SA 2.0

Originally published on Mango Developer.