<div dir="ltr">For kicks I looked at the code in a commercial product that we are open sourcing very soon at Continuum Analytics.  It has about 27k lines of Python and Cython.  I checked it just by running `<span style="font-size:14px">grep -C3 'if.*None'` over the source code and eyeballing.</span><div><br></div><div>Obviously, this code might be refactored if other syntax was available.  And I personally wrote only a small part of the code, so I might write it differently either way.  But looking through, the uses of 'is (not) None' I found fell into a few categories.  </div><div><br></div><div>Some were suites of statements under the 'foo is None' branch.  Those would have to remain suites.  Some are single line returns, other single line raise.  Those I call 'value' are the ones that would lend themselves to the 'bar = foo ?? baz' style.  The ones I call 'attribute' are the cases where we test for non-None before accessing an attribute of the object.</div><div><br></div><div>The thing that I found surprisingly common was cases where a compound condition was used, e.g. 'if x is None and y > 37'.  Those might be possible to refactor, but cannot be directly rewritten in the coalescing style.<br><div><br></div><div><div><div style="font-size:14px">  - Suite: 75</div><div style="font-size:14px">  - Return: 21</div><div style="font-size:14px">  - Raise: 13</div><div style="font-size:14px">  - Value: 46</div><div style="font-size:14px">  - Attribute: 2</div><div style="font-size:14px">  - Compound condition: 25</div></div></div></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Sat, Sep 10, 2016 at 7:15 PM, David Mertz <span dir="ltr"><<a href="mailto:mertz@gnosis.cx" target="_blank">mertz@gnosis.cx</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><p dir="ltr">How much of the time is a branch of the None check a single fallback value or attribute access versus how often a suite of statements within the not-None branch?</p>
<p dir="ltr">I definitely check for None very often also. I'm curious what the breakdown is in code I work with.</p><div class="HOEnZb"><div class="h5">
<div class="gmail_extra"><br><div class="gmail_quote">On Sep 10, 2016 7:10 PM, "Guido van Rossum" <<a href="mailto:guido@python.org" target="_blank">guido@python.org</a>> wrote:<br type="attribution"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">To the contrary. I read and write code that performs explicit checks<br>
for None all the time. Catching AttributeError is often a code smell<br>
or at least a measure of last resort.<br>
<br>
On Sat, Sep 10, 2016 at 6:46 PM, David Mertz <<a href="mailto:mertz@gnosis.cx" target="_blank">mertz@gnosis.cx</a>> wrote:<br>
> Ok, I have been thinking of the behavior too broadly. I realize now that<br>
> `x?.foo` might still simply raise an AttributeError if x is neither None nor<br>
> a thing with a foo attribute.<br>
><br>
> The class I wrote is definitely too aggressive for the behavior described.<br>
> On the other hand, by being narrower in behavior there feels like even less<br>
> motivation for new syntax.<br>
><br>
><br>
> On Sep 10, 2016 6:29 PM, "MRAB" <<a href="mailto:python@mrabarnett.plus.com" target="_blank">python@mrabarnett.plus.com</a>> wrote:<br>
>><br>
>> On 2016-09-11 02:02, David Mertz wrote:<br>
>>><br>
>>> On Sat, Sep 10, 2016 at 5:23 PM, Guido van Rossum <<a href="mailto:guido@python.org" target="_blank">guido@python.org</a><br>
>>> <mailto:<a href="mailto:guido@python.org" target="_blank">guido@python.org</a>>> wrote:<br>
>>><br>
>>>     No. PEP 505 actually solves the problem without ever catching<br>
>>>     AttributeError. Please read it.<br>
>>><br>
>>><br>
>>> I read it again (I did a year ago, but reviewed it now).  I hadn't been<br>
>>> thinking that the *mechanism* of a new None-coalescing operator would<br>
>>> actually be catching an exception.  It could (and should) work<br>
>>> differently if it becomes syntax.<br>
>>><br>
>>> What I was getting at with "essentially" was that it would *do the same<br>
>>> thing* that an AttributeError does.  That is, if `x.foo` can't be<br>
>>> evaluated (i.e. x doesn't have an attribute 'foo'), then access is<br>
>>> informally "an error."  The hypothetical "x?.foo" catches that "error"<br>
>>> and substitutes a different value.  The particular implementation<br>
>>> under-the-hood is less important for most programmers who might use the<br>
>>> construct (and I think documentation would actually give an informal<br>
>>> equivalent as something similar to what I put in the NoneCoalesce class).<br>
>>><br>
>> x?.foo would lookup attribute 'foo' _unless_ x was None, in which case it<br>
>> would return None. It's simply:<br>
>><br>
>>     None if x is None else x.foo<br>
>><br>
>> This means that None?.__str__() would return None, not 'None'. (None has<br>
>> an attribute called '__str__', and None.__str__() returns 'None', but it<br>
>> would not be looked up because None is, well, None.)<br>
>><br>
>> ______________________________<wbr>_________________<br>
>> Python-ideas mailing list<br>
>> <a href="mailto:Python-ideas@python.org" target="_blank">Python-ideas@python.org</a><br>
>> <a href="https://mail.python.org/mailman/listinfo/python-ideas" rel="noreferrer" target="_blank">https://mail.python.org/mailma<wbr>n/listinfo/python-ideas</a><br>
>> Code of Conduct: <a href="http://python.org/psf/codeofconduct/" rel="noreferrer" target="_blank">http://python.org/psf/codeofco<wbr>nduct/</a><br>
><br>
><br>
> ______________________________<wbr>_________________<br>
> Python-ideas mailing list<br>
> <a href="mailto:Python-ideas@python.org" target="_blank">Python-ideas@python.org</a><br>
> <a href="https://mail.python.org/mailman/listinfo/python-ideas" rel="noreferrer" target="_blank">https://mail.python.org/mailma<wbr>n/listinfo/python-ideas</a><br>
> Code of Conduct: <a href="http://python.org/psf/codeofconduct/" rel="noreferrer" target="_blank">http://python.org/psf/codeofco<wbr>nduct/</a><br>
<br>
<br>
<br>
--<br>
--Guido van Rossum (<a href="http://python.org/~guido" rel="noreferrer" target="_blank">python.org/~guido</a>)<br>
</blockquote></div></div>
</div></div></blockquote></div><br><br clear="all"><div><br></div>-- <br><div class="gmail_signature" data-smartmail="gmail_signature">Keeping medicines from the bloodstreams of the sick; food <br>from the bellies of the hungry; books from the hands of the <br>uneducated; technology from the underdeveloped; and putting <br>advocates of freedom in prisons.  Intellectual property is<br>to the 21st century what the slave trade was to the 16th.<br></div>
</div>