@@ -14,15 +14,15 @@ Abstract
1414
1515This PEP proposes adding two new operators.
1616
17- * The "``None `` coalescing" operator ``?? ``
17+ * The "``None ``- coalescing" operator ``?? ``
1818* The "Coalescing assignment" operator ``??= ``
1919
20- The ``None `` coalescing operator evaluates the left hand side,
21- checks if it is ``not None `` and returns the result. If the value is
22- ``None ``, the right hand side is evaluated and returned.
20+ The ``None ``- coalescing operator evaluates the left hand side,
21+ checks if it is ``not None `` and, if so, returns the result. If the
22+ value is ``None ``, the right hand side is evaluated and returned.
2323
2424The coalescing assignment operator will only assign the right hand side
25- if the left hand side evaluates to ``None ``.
25+ to the left hand side if the left hand side evaluates to ``None ``.
2626
2727They are roughly equivalent to:
2828
@@ -42,14 +42,15 @@ Motivation
4242==========
4343
4444First officially proposed ten years ago in (the now deferred) :pep: `505 `,
45- the idea to add coalescing operators has been along for some time now,
46- discussed at length in numerous threads, most recently in
45+ the idea to add `` None ``- coalescing operators has been along for some
46+ time now, discussed at length in numerous threads, most recently in
4747[#discuss_revisit_505 ]_. This PEP aims to capture the current state of
4848discussion and proposes a specification for addition to the Python
4949language. In contrast to :pep: `505 `, it will only focus on the two
50- coalescing operators. See the `Deferred Ideas `_ section for more details.
50+ ``None ``-coalescing operators. See the `Deferred Ideas `_ section for
51+ more details.
5152
52- ``None `` coalescing operators are not a new invention. Several other
53+ ``None ``- coalescing operators are not a new invention. Several other
5354modern programming languages have so called "``null `` coalescing"
5455operators, including TypeScript [#ts ]_, ECMAScript (a.k.a. JavaScript)
5556[#js_1 ]_ [#js_2 ]_, C# [#csharp ]_, Dart [#dart_1 ]_ [#dart_2 ]_,
@@ -61,7 +62,7 @@ checking for truthiness, looks for ``None`` values.
6162Explicit checks for ``None ``
6263----------------------------
6364
64- In Python ``None `` is often used to denote the absents of a value. As
65+ In Python ``None `` is often used to denote the absence of a value. As
6566such it it common to have to check if a value is ``None `` to do some
6667action or provide a fallback value. Python provides several options to
6768do that, each with their own advantages and disadvantages. One such
@@ -77,7 +78,7 @@ option is a conditional statement:
7778 print (f " The value is { s.lower()} " )
7879
7980 While the intent is quite clear, the conditional statement is verbose
80- and often requires repeating the variable expression. An common
81+ and often requires repeating the variable expression. A common
8182alternative is the conditional expression:
8283
8384.. code-block :: python
@@ -128,7 +129,7 @@ values other then ``None``, too. Common ones include ``0``, ``""``,
128129``() ``, ``[] ``, ``{} ``, ``set() `` or custom objects which overwrite
129130``__bool__ ``.
130131
131- The "``None `` coalescing" operator can bridge this gab by adding an
132+ The "``None ``- coalescing" operator can bridge this gab by adding an
132133alternative to ``or `` which explicitly checks only for ``None `` values.
133134Adding ``?? `` can keep the expression concise while clearly
134135communicating the intent:
@@ -168,7 +169,7 @@ helpful for more complex expressions.
168169::
169170
170171 def other(obj):
171- if obj.var.some_other_object.another_variable is not None:
172+ if obj.var.some_other_object.another_variable is None:
172173 obj.var.some_other_object.another_variable = "fallback"
173174
174175 # can be replaced with
@@ -178,7 +179,7 @@ helpful for more complex expressions.
178179Specification
179180=============
180181
181- The ``None `` coalescing operator
182+ The ``None ``- coalescing operator
182183--------------------------------
183184
184185The ``?? `` operator is added. It first evaluates the left hand side.
@@ -285,7 +286,7 @@ is extended to include the coalesce assignment.
285286 Backwards Compatibility
286287=======================
287288
288- The coalescing operators are **opt-in **. Existing programs will
289+ The `` None ``- coalescing operators are **opt-in **. Existing programs will
289290continue to run as is. So far code which used either ``?? `` or ``??= ``
290291raised a ``SyntaxError ``.
291292
@@ -299,8 +300,8 @@ There are no new security implications from this proposal.
299300How to Teach This
300301=================
301302
302- In a practical sense it might be helpful to think of the "``None ``
303- coalescing" operator ``?? `` as a special case for the conditional ``or ``
303+ In a practical sense it might be helpful to think of the "``None ``-coalescing"
304+ operator ``?? `` as a special case for the conditional ``or ``
304305operator, with the caveat that ``?? `` checks for ``is not None `` instead
305306of truthiness. As such it makes sense to include ``?? `` when teaching
306307about the other conditional operators ``and `` and ``or ``.
@@ -325,9 +326,9 @@ Deferred Ideas
325326-------------------------------
326327
327328:pep: `505 ` also suggest the addition of the ``None ``-aware access
328- operators ``?. `` and ``?[ ] ``. As the coalescing operators have their
329- own use cases, the ``None ``-aware access operators were moved into a
330- separate document, see PEP-823. Both proposals can be adopted
329+ operators ``?. `` and ``?[ ] ``. As the `` None ``- coalescing operators have
330+ their own use cases, the ``None ``-aware access operators were moved
331+ into a separate document, see PEP-823. Both proposals can be adopted
331332independently of one another.
332333
333334
@@ -376,7 +377,7 @@ is similar to other binary operators like ``+`` or ``**``. This is not
376377the case. While binary operators first evaluate the left **and ** right
377378hand side before performing the operation, for ``?? `` only the left hand
378379side is evaluated if the values is ``not None ``. As such the
379- "``None `` coalescing" operator is much more closely related to the
380+ "``None ``- coalescing" operator is much more closely related to the
380381conditional operators ``or `` and ``and `` which also short-circuit the
381382expression for truthy and falsy values respectively.
382383
@@ -402,13 +403,13 @@ Common objections
402403Just use a conditional expression
403404---------------------------------
404405
405- The coalescing operators can be considered syntactic sugar for
406- existing conditional expressions and statements. As such some
406+ The `` None ``- coalescing operators can be considered syntactic sugar
407+ for existing conditional expressions and statements. As such some
407408questioned whether they would add anything meaningful to the
408409language as a whole.
409410
410411As shown in the `Motivation `_ section, there are clear benefits to
411- using the coalescing operators. To summarize them again:
412+ using the `` None ``- coalescing operators. To summarize them again:
412413
413414- They help avoid repeating the variable expression or having to
414415 introduce a temporary variable.
@@ -425,9 +426,9 @@ Proliferation of ``None`` in code bases
425426---------------------------------------
426427
427428One of the reasons why :pep: `505 ` stalled was that some expressed their
428- concern how coalescing and ``None ``-aware operators will affect the code
429- written by developers. If it is easier to work with ``None `` values,
430- this will encourage developers to use them more. They believe that
429+ concern how `` None ``- coalescing and ``None ``-aware operators will affect
430+ the code written by developers. If it is easier to work with ``None ``
431+ values, this will encourage developers to use them more. They believe that
431432e.g. returning an optional ``None `` value from a function is usually an
432433anti-pattern. In an ideal world the use of ``None `` would be limited as
433434much as possible, for example with early data validation.
@@ -436,18 +437,18 @@ It is certainly true that new language features affect how the language
436437as a whole develops. Therefore any changes should be considered carefully.
437438However, just because ``None `` represents an anti-pattern for some, has
438439not prevented the community as a whole from using it extensively. Rather
439- the lack of coalescing operators has stopped developers from writing
440- concise expressions and instead often leads to more complex code or such
441- which can contain subtle errors, see the `Motivation `_ section for more
442- details.
440+ the lack of `` None ``- coalescing operators has stopped developers from
441+ writing concise expressions and instead often leads to more complex code
442+ or such which can contain subtle errors, see the `Motivation `_ section
443+ for more details.
443444
444445``None `` is not special enough
445446------------------------------
446447
447448Some mentioned that ``None `` is not special enough to warrant dedicated
448449operators.
449450
450- Coalescing operators have been added to a number of other modern
451+ `` None ``-coalescing operators have been added to a number of other modern
451452programming languages. Furthermore, adding ``?? `` and ``??= `` is
452453something which was suggested numerous times since :pep: `505 ` was
453454first proposed ten years ago.
0 commit comments