Add None coalescing operator in Python

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