[Python-ideas] Null coalescing operator

Kyle Lahnakoski klahnakoski at mozilla.com
Wed Nov 2 22:22:56 EDT 2016


On 11/2/2016 2:30 PM, Zero Piraeus wrote:
>
> If I write something like obj.attr, the failure mode I care about is that
> obj has no attribute attr, rather than that obj is specifically None (or
> one of a defined group of somewhat Nonelike objects).
>

I agree with this understanding.  The problem with None-coalescing is it 
doesn't consider where None came from.  I suspect enumerating the source 
of None values will reveal the majority of them are a result of 
`getattr(obj, attr)` returning None (or obj.get(attr) returning None).

If your code deals with annotated objects, rather than strictly typed 
objects, you will have many instances of attributes resolving to None. 
Making specific classes for each of the annotations, or combinations of 
annotations, is prohibitive, so you don't.  Rather, you use a few 
general types and set some attributes to None to indicate a property is 
not-relevant-for-this-object.

None checks are type checks. They are late type checks .






More information about the Python-ideas mailing list