[Python-ideas] Null coalescing operators

Steven D'Aprano steve at pearwood.info
Tue Sep 22 05:15:08 CEST 2015


On Mon, Sep 21, 2015 at 05:23:42PM -0400, Terry Reedy wrote:

> I agree with Paul Moore that propagating None is generally a bad idea. 

As I understand it, you and Paul are describing a basic, simple idiom 
which is ubiquitous across Python code: using None to stand in for "no 
such value" when the data type normally used doesn't otherwise have 
something suitable. Consequently I really don't understand what you and 
Paul have against it.


> It merely avoids the inevitable exception.

I think you meant to say it merely *postpones* the inevitable exception. 
But that's wrong, there's nothing inevitable about an exception here.

It's not *hard* to deal with "value-or-None". It's just tedious, which 
is why a bit of syntactic sugar may appeal.


[...]
> 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.

In one of my earlier posts, I discussed this Null object design pattern. 
I think it is an anti-pattern. If people want to add one to their own 
code, it's their foot, but I certainly don't want to see it as a 
built-in. Thank goodness Guido has already ruled that out :-)


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

I don't think that is the motivation of the original proposal, nor is it 
one I particularly care about.

I think that there is a level of inconvenience below which it's not 
worth adding yet more syntax just to save a few characters. That 
inconvenience is not necessarily just to do with the typing, it may be 
conceptual, e.g. we have "x != y" rather than "not x == y". I think that

    x is not None

fails to reach that minimum level of inconvenience to justify syntactic 
sugar, but 

    obj.method() if x is not None else None

does exceed the level. So I am mildly interested in null-coalescing 
versions of attribute and item/key lookup, but not at all interested in 
making the "x is not None" part *alone* shorter.


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

Bring it back to the original post's motivating use-case. Slightly 
paraphrased, it was something like:

    value = obj.method() if obj is not None else None

Having x! as a short-cut for "x is not None" makes this a bit shorter to 
write:

    value = obj.method() if obj! else None

but it is still very boilerplatey and verbose compared to the suggested:

    value = obj?.method()



-- 
Steve


More information about the Python-ideas mailing list