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
6 Likes

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.

https://mail.python.org/archives/list/python-ideas@python.org/thread/6NHOUO2IJ64E6XP6VA3QA7YYBEMBBUM3/

https://mail.python.org/pipermail/python-ideas/2015-September/036289.html

1 Like

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.

5 Likes

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.

3 Likes

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
nor.

5 Likes

What about alt keyword, as it means alternative?

I think this will be good for Python to have, since javascript and flutter and kotlin and c# has already implemented it:

a_variable = None

new_variable = a_variable ?? 'Hello Python'

assert new_variable == 'Hello Python'

This is often spelled:

 new_variable = a_variable or 'Hello Python'

in Python. It doesn’t work if, say, '' is a valid value but is
otherwise effective. So we’ve got maybe 90% (wild guess) of the use
cases covered with just that.

Cheers,
Cameron Simpson cs@cskk.id.au

I doubt it is as high as 90%.

But whatever the percentage, the same argument applies to the ternary if operator. For many, many years we rejected every request for a ternary if operator with the argument that you can just write

condition and if_true_value or if_false_value

E.g. len(mylist) > 0 and mylist[0] or "default". Can you see the bug? (Hint: what if the value of mylist[0] is, say, the empty string?).

And then one day GvR got badly bitten by that idiom, and just like that we added a ternary if operator.

4 Likes

What was the justification for rejecting maybe here? It doesn’t strike me as anything odd if you think about it:

data = [] if data is None else data
files = [] if files is None else files
headers = {} if headers is None else headers

to

data = maybe data else []
files = maybe files else []
headers = maybe headers else {}
1 Like

The usual arguments against introducing new keywords are that they’re likely already on use as identifiers by users, and that it’s another keyword for users to remember.

In this case, there’s also prior art in other languages to achieve the same outcome through other syntax.

3 Likes

“y if x is None else x” is more readable than “x ?? y”.

Since code is written once and read 100 times, it’s probably worth the extra typing.