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