Add None coalescing operator in Python

Just like in Javascript there is Nullish coalescing operator(??) for checking for null values, it would be a nice addition to python as python also hasNone .

Currently users often check for None with if statement:

if foo is not None:
    return bar

Or using or :

foo or bar

With or the problem is that it not only check for None but also other bool false values like empty string or False or empty list etc

Adding ?? in python for checking would result in much simpler, which would only check for None values:

foo ?? bar

Hi Kumar,

There has been a PEP and a huge amount of discussion about this.

Here are only a few of the threads discussing this idea. There are
probably others.

Hi @steven.daprano,

Thanks for the links and the PEP, there has been a recent addition to the python syntax i.e. pattern matching so it might be a better time to think about adding this feature to python 3.10, as seeing the PEP it is deferred but not rejected so it might be worth to have a look again.

Unfortunately, there is absolutely no way there is enough time to add such a feature to Python 3.10 :slight_smile:

@pablogsal can it be discussed and possibly implemented for python 3.11 also I am only about adding ?? for None not others in the pep

For that a PEP needs to be written/re-written, discussed in python-dev and then submitted to the Steering Council

@steve.dower Do you have any plans for this PEP as you were the author for the pep ?

My plan is to drop it completely, because I now think the additional syntax would not make Python cleaner or a better language.


Yes, I agree with @steve.dower . The readability would be less (to quote PEP 20 “Readability counts”), it’s less obvious (particularly to new programmers), it becomes difficult to teach (my perspective). I’m all for the “less is more”; more syntax, more keywords, a bigger language are all negatives in my view. Cf Perl 5 to Perl 6. I do have a “safe” function that I use, particularly when dealing with SQL (null) and json, for dealing with this issue.

I agree that using ??, ?. and ?[ would make Python look more “cryptic”, as Python programs typically look like readable pseudocode (e.g. prefer English words rather than characters as operators). I do have an idea of an alternative spelling of the ?? grammar. Now that we have soft keywords in PEG grammar, we can add a new soft keyword nor, and a nor b nor c is an alternative spelling of a ?? b ?? c. This could be more readable and doesn’t make use of the ? character. By defining nor as a soft keyword, existing code using nor as a variable name will continue to work. The drawback is that this doesn’t give us an alternative spelling for ?. and ?[, although I personally feel that ?? is the most needed one. Currently any correct implementation of a ?? b ?? c (short circuiting behavior is desired) would be verbose.

The problem there being that nor is already a common English conjunction, used for example:

I don’t want ice-cream, nor want chocolate, nor want candy.

In your example, I would read that as a and not lb and not c, or replace nor with and not

Maybe it’s me not being a native English speaker, but

a ?? b ?? c
a nor b nor c

both read like alien text to me. I can understand neither of them.


I don’t think that “nor” is an appropriate word to use.

In standard English, it is a synonym for “and not”. (Nobody ever said
that English was logical!) Normally it is used in the form “neither here
nor there”. For an English speaker, it would be odd to see “nor” used in
a specialist form like a coalescing operator.

More importantly, “nor” is also a logical (boolean) operator:

I think that programmers with even a passing knowlege of boolean logic
would be very confused by a nor operator that didn’t perform logical


What about alt keyword, as it means alternative?