PEP 824 – None-coalescing operators | peps.python.org
PEP 824 – None-coalescing operators PEP 824 – None-coalescing operators Author : Marc Mueller Sponsor : Guido van Rossum <guido at python.org> Discussions-To : Discourse thread Status : Draft Type : Standards Track Created : 20-Sep-2026 Python-Version : 3.16 Post-History : 22-Sep-2026 Table of Contents Abstract Motivation Explicit checks for None Overwrite None values Defaults for function arguments Specification The None -coalescing operator Precedence AST changes Grammar changes The None -coalescing assignment operator Subexpression caching AST changes Grammar changes Backwards Compatibility Security Implications How to Teach This Reading expressions out loud None -coalescing operator None -coalescing assignment operator Reference Implementation Deferred Ideas None -aware access operators Other conditional assignment operators Rejected Ideas Add new (soft-) keyword Add ?? as a binary operator Add ??= as AugAssign Make ??= atomic Common objections Just use a conditional expression Proliferation of None in code bases None is not special enough There are better default values than None Use custom sentinels instead of None Late-bound function argument defaults Footnotes Copyright Abstract 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 Motivation First officially proposed over ten years ago in (the now deferred) PEP 505 , the idea to add None -coalescing operators has been along for some time now, discussed at length in numerous threads, most recently in [1] and [2] . This PEP aims to capture the current state of discussion and proposes a specification for addition to the Python language.
In contrast to PEP 505 , it will only focus on the two None -coalescing operators. See the Deferred Ideas section for more details. None -coalescing operators are not a new invention. Several other modern programming languages have so called “ null coalescing” operators, including TypeScript [3] , ECMAScript (a.k.a. JavaScript) [4] [5] , C# [9] , Dart [11] [12] , Swift [13] , Kotlin [14] , PHP [15] [16] and more. The general idea is to provide a conditional operator, similar to or , which instead of truthiness, checks for None values.
Explicit checks for None It is common in Python to use None as a default value that cannot be confused with other inherent default values like [] , "" or 0 . Most code working with such functions either does some kind of validation to make sure a valid value is actually returned, maybe returning early or raising an exception if it is not, or provides a default / fallback value. To do this, it is common to use is None / is not None checks. class User : name : str | None @property def age ( self ) -> int | None : ... def show_user_age ( user : User ): age = user . age if age is None : age = "unknown" print ( f "The user age is { age } " ) Though the intent is clear, it is quite verbose.
The target name has to be repeated multiple times just to be able to assign a default value to the target variable. It is possible to write this in a more concise form using an if-expression. However, that has its own issues. def show_user_age ( user : User ): age = user . age if user . age is not None else "unknown" print ( f "The user age is { age } " ) Especially for fairly simple value expressions, it is not uncommon to just repeat them, like in the example above, instead of using an assignment expression.
This could cause problems if the value expression has side effects which would be executed twice now. Furthermore, there is not a single agreed upon way to write these expressions. Instead of putting the value first, some might prefer to invert the check to use is None instead. Reader need to constantly be aware of these challenges, increasing the mental load. Using the “ None -coalescing” operator ?? instead, helps to keep the expression short and predictable while still clearly communicating the intent. def show_user_age ( user : User ): age = user . age ??
"unknown" print ( f "The user age is { age } " ) Overwrite None values Sometimes it might be necessary to assign a fallback value inside an object. To do so, the expression is usually written twice. Once for the is None check, and again for the assignment. def fix_user_name ( user : User ): if user . name is None : user . name = "unknown" Using the “ None -coalesce assignment” operator ??= helps to avoid repeating the expression.
Especially for more complex once, this will make it easier to read and write. def fix_user_name ( user : User ): user . name ??= "unknown" Defaults for function arguments Function argument defaults are evaluated in the parent scope. That is a common issue in cases where the default value is a mutable object or depends on the function context itself. In these cases, a typical solution is to allow None as argument and assign the fallback value inside the function itself. def show_user_name ( user : User | None ): if user is None : user = create_default_user () print ( f "The user name is { user . name } " ) This could be rewritten as: def show_user_name ( user : User | None ): user ??= create_default_user () print ( f "The user name is { user . name } " ) Specification The None -coalescing operator The ?? operator is added.
It first evaluates the left-hand side. The result is cached, so that the expression is not evaluated again. If the value is not None , the cached result is returned. If it is None , the right-hand side expression is evaluated and returned instead. # a ?? b _t if (( _t := a ) is not None ) else b Precedence The precedence of ?? will be lower than or but higher than conditional expressions. Parentheses can be added as necessary to modify the precedence of individual expressions.
A few examples of how implicit parentheses would be placed: # x or y ?? 2 ( x or y ) ?? 2 # "Hello" if x ?? True else 0 "Hello" if ( x ?? True ) else 0 AST changes A new Coalesce operator is added to boolop for use in BoolOp nodes. expr = BoolOp ( boolop op , expr * values ) | ... boolop = And | Or | Coalesce Grammar changes A new ?? token is added, as well as a new coalesce rule. Every rule which previously referenced the disjunction rule is updated to refer to the coalesce rule instead. coalesce : | disjunction ( '??' disjunction ) + | disjunction disjunction : | conjunction ( 'or' conjunction ) + | conjunction The None -coalescing assignment operator The ??= operator is added.
It performs a conditional assignment. As such it will first evaluate the left-hand side and check that the value is None and only then evaluate and assign the result from the right-hand side. If the first value is not None , the evaluation and assignment of the right-hand side are skipped. # a ??= b if a is None : a = b Subexpression caching Subexpressions on the left-hand side are only evaluated once before being cached.
This is similar to augmented assignments. # a.b.c ??= d if ( _t1 := a . b ) . c is None : _t1 . c = d # func().var[other()] ??= x if ( _t2 := func () . var )[( _t3 := other ())] is None : _t2 [ _t3 ] = x AST changes A new BoolAssign AST node is added. Similar to AugAssign , it stores a target and value expression as well as the boolean operator Coalesce . stmt = ... | AugAssign ( expr target , operator op , expr value ) | BoolAssign ( expr target , boolop op , expr value ) Grammar changes A new ??= token is a
The rest of this page is on the live site.