freenode
Languages & Toolchains

PEP 823 proposes None-aware ?. and ?[] operators for Python

The draft splits long-running PEP 505 ideas so access operators can be judged apart from coalescing ones.

Python may gain two new operators for traversing optional values without raising exceptions. PEP 823, posted by cdce8p on discuss.python.org, proposes None-aware attribute access (?.) and None-aware indexing (?[]). Both evaluate the left-hand side, proceed only when it is not None, and otherwise yield None while skipping the rest of the expression, including argument and subscript evaluation.

The draft revives material from PEP 505 and is paired with companion PEP 824 on coalescing operators. The split is deliberate: either proposal can be accepted without the other. An interactive demo accompanies the texts.

Supporters describe the operators as relief from repetitive null checks in nested optional data, such as entity-relationship models where a customer may be absent or a name field unfilled. Several readers coming from TypeScript said the syntax fills a gap they already rely on elsewhere. The choice to special-case only None, rather than arbitrary sentinels or attribute errors, drew approval from developers who want a narrow, predictable rule.

Others were cooler. Some said they rarely need the pattern because they keep nested optionals out of their data models, or they prefer ordinary if checks and structural pattern matching. Critics worry shorter syntax would encourage more Optional fields in APIs and that the operators’ short-circuiting can hide side effects in skipped tails. Alternatives floated included a unary postfix ? meaning “is not None.” Petr Viktorin also flagged that IPython already uses ? for help, so adopting the operator would constrain similar uses in CPython’s REPL.

Paul Moore framed the divide as partly cultural: familiarity with TypeScript-style optional chaining shapes how people design code, while long-time Python practice often structures data so the operators are unnecessary. Whether that is reason enough to add them is left for the Steering Council.