[Python-ideas] Null coalescing operators

Terry Reedy tjreedy at udel.edu
Mon Sep 21 23:23:42 CEST 2015


On 9/21/2015 1:07 PM, Guido van Rossum wrote:

> I apologize for having misunderstood the status of your PEP. I think it
> would be great if you finished the PEP. As you know the ? operator has
> its share of fans as well as detractors, and I will happily wait until
> more of a consensus appears.

Add me to the detractors of what I have read so far ;-).

In arithmetic, 1/0 and 0/0 both stop the calculation.  My hand 
calculator literally freezes until I hit 'on' or 'all clear'.  Early 
computers also stopped, maybe with an instruction address and core dump. 
  Three orthogonal solutions are: test y before x/y, so one can do 
something else; introduce catchable exceptions, so one can do something 
else; introduce contagious special objects ('inf' and 'nan'), which at 
some point can be tested for, so one can do something else.  Python 
introduced 'inf' and 'nan' but did not use them to replace 
ZeroDivisionError.

Some languages lacking exceptions introduce a contagious null object. 
Call it Bottom.  Any operation on Bottom yields Bottom.  Python is not 
such a language. None is anti-contagious; most operations raise an 
exception.

I agree with Paul Moore that propagating None is generally a bad idea. 
It merely avoids the inevitable exception.  Or is it inevitable? Trying 
to avoid exceptions naturally leads to the hypergeneralization of 
allowing '?' everywhere.

Instead of trying to turn None into Bottom, I think a better solution 
would be a new, contagious, singleton Bottom object with every possible 
special method, all returning Bottom. Anyone could write such for their 
one use.  Someone could put it on pypi to see if there how useful it 
would be.

I agree with Ron Adam that the narrow issue is that bool(x) is False is 
sometimes too broad and people dislike of spelling out 'x is not None'. 
So abbreviate that with a unary operator; 'is not None', is a property 
of objects, not operators. I think 'x!' or 'x?', either meaning 'x is 
not None', might be better than a new binary operator. The former, x!, 
re-uses ! in something close to its normal meaning: x really exists.

-- 
Terry Jan Reedy



More information about the Python-ideas mailing list