Python 1 min read

PEP 823 and PEP 824 Propose Four New Null-Handling Operators for Python

New Python enhancement proposals introduce optional chaining and null-coalescing operators to simplify code by reducing explicit checks for None values.

Python Enhancement Proposals PEP 823 and PEP 824 introduce four new operators to handle None values. PEP 823 adds the ?. operator for attribute access and the ?[] operator for indexing. Both check if the left-hand side is None before proceeding. If so, the expression short-circuits and returns None instead of raising an exception.

PEP 824 introduces the ?? and ??= operators for assignment and value selection. The ?? operator checks specifically for None. If the left side is not None, it returns that value; otherwise, it evaluates and returns the right side. This differs from the or operator, which triggers on standard truthiness. The companion ??= operator assigns the right-hand side value to the left-hand variable only if the current value of the left-hand side is None. These additions reduce the need for explicit conditional checks, replacing verbose patterns that currently require temporary variables to ensure an object is valid before you access its attributes or members.