<div dir="ltr">Both this discussion, PEP 505, and the one a year ago, tend to mix up 2 related but separate proposals:<div>w</div><div>(A) Add a None-coalescing operator (like C# a ?? b, what you would write in Python as "a or b" if it didn't have the falsy gotcha)</div><div>(B) Add some None-aware navigation operators ( The "?.", "?()", "?[]", or what you would write in python as "a and a.attribute" if it didn't have the falsy gotcha)</div><div><br></div><div>Both are things that can be already done in python, so the purpose here is to add some convenience (aka "syntax sugar"). IMO, this kind of syntax sugar proposals should be weighed with the frequency of the coding pattern where the sugar can be applied. And from the stats presented in PEP-505 (B) is one order of magnitude less usual than (A); that matches most of the examples I see in the threads and FWIW my personal experience.</div><div><br></div><div>So, as a counterproposal I would like to suggest:</div><div><br></div><div>* Add an "a ?? b" operator which is equivalent to "a if a is None else b" (but evaluating a once)</div><div>* Do not add none-aware navigation; in the less usual scenario where you need to do it AND ALSO the "and" operator is not usable (it frequently is, given that by default classes are truish), well, you can use a ternary operator</div><div>* I don't care if it's an alternate syntax (I'm surprised nobody suggested "||" which is also used in other languages for similar purpose)</div><div><br></div><div>Would this satisfy most of the people requesting this? (and, would it satisfy the people making the decision?)</div><div><br></div><div><br></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Sun, Sep 11, 2016 at 4:45 AM, Bruce Leban <span dir="ltr"><<a href="mailto:bruce@leban.us" target="_blank">bruce@leban.us</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div class="gmail_extra"><span class=""><br><div class="gmail_quote">On Sat, Sep 10, 2016 at 6:02 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:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div>What I was getting at with "essentially" was that it would *do the same thing* that an AttributeError does.  That is, if `x.foo` can't be evaluated (i.e. x doesn't have an attribute 'foo'), then access is informally "an error."  The hypothetical "x?.foo" catches that "error" and substitutes a different value.  The particular implementation under-the-hood is less important for most programmers who might use the construct (and I think documentation would actually give an informal equivalent as something similar to what I put in the NoneCoalesce class).</div><div></div></blockquote></div><br></span>That's not a good way to think about it. This new operator is only checking for None and not actually checking for AttributeErrors. Consider:</div><div class="gmail_extra"><br></div><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><div class="gmail_extra"><font face="monospace, monospace">(3).x    # AttributeError</font></div><div class="gmail_extra"><font face="monospace, monospace">{}.x     # AttributeError</font></div></blockquote><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><div class="gmail_extra"><font face="monospace, monospace">None.x   # AttributeError</font></div></blockquote><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><div class="gmail_extra"><font face="monospace, monospace"><br></font></div><div class="gmail_extra"><font face="monospace, monospace">(3)?.x   # still AttributeError</font></div></blockquote><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><div class="gmail_extra"><font face="monospace, monospace">{}?.x    # still AttributeError</font></div></blockquote><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><div class="gmail_extra"><font face="monospace, monospace">None?.x  # None</font></div></blockquote><br><div><div class="gmail_extra">And also:</div><div class="gmail_extra"><br></div></div><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><div><div class="gmail_extra"><div><div><font face="monospace, monospace">None.__class__   # </font><span style="font-family:monospace,monospace"><type 'NoneType'></span></div></div></div></div></blockquote><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><div><div class="gmail_extra"><div><span style="font-family:monospace,monospace">None?.__class__  # None</span></div></div></div></blockquote><div><div class="gmail_extra"><br></div><div class="gmail_extra">And it's certainly not the case that those values don't accept any attributes: <div><br></div><div><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><div><div><font face="monospace, monospace">(3).real        # </font><span style="font-family:monospace,monospace">3</span></div><div><font face="monospace, monospace">{}.value</font><span style="font-family:monospace,monospace">s       # </span><span style="font-family:monospace,monospace"><built-in method ...></span></div><div><font face="monospace, monospace">None.__class__  #</font><span style="font-family:monospace,monospace"><type 'NoneType'></span></div></div><div><br></div></blockquote></div><div><div><div dir="ltr"><div><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><font face="arial, helvetica, sans-serif">--- Bruce<br></font><div><div><font face="arial, helvetica, sans-serif">Check out my puzzle book and g</font><span style="font-family:arial,helvetica,sans-serif;font-size:12.8px">et it free here:</span></div><div><a href="http://J.mp/ingToConclusionsFree" style="font-family:arial,helvetica,sans-serif;font-size:12.8px" target="_blank">http://J.mp/<wbr>ingToConclusionsFree</a><span style="font-family:arial,helvetica,sans-serif;font-size:12.8px"> (<wbr>available on iOS)</span></div></div><div><br></div><div><br></div></div></div></div></div></div></div></div></div></div></div></div>
</div></div></div>
<br>______________________________<wbr>_________________<br>
Python-ideas mailing list<br>
<a href="mailto:Python-ideas@python.org">Python-ideas@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/python-ideas" rel="noreferrer" target="_blank">https://mail.python.org/<wbr>mailman/listinfo/python-ideas</a><br>
Code of Conduct: <a href="http://python.org/psf/codeofconduct/" rel="noreferrer" target="_blank">http://python.org/psf/<wbr>codeofconduct/</a><br></blockquote></div><br><br clear="all"><div><br></div>-- <br><div class="gmail_signature" data-smartmail="gmail_signature"><div dir="ltr"><div><div dir="ltr">Daniel F. Moisset - <span style="font-size:small">UK Country Manager</span><div><a href="http://www.machinalis.com" target="_blank">www.machinalis.com</a></div><div>Skype: @dmoisset</div></div></div></div></div>
</div>