<div dir="auto"><div>True enough. I remember the prior discussion, but didn't look up the PEP number. I know it's hard to revisit these ideas, but occasionally it works, especially if a nicer spelling is found (Barry proposed some a bit different than my ideas)<br><div class="gmail_extra"><br><div class="gmail_quote">On Nov 29, 2017 9:55 AM, "Eric V. Smith" <<a href="mailto:eric@trueblade.com">eric@trueblade.com</a>> wrote:<br type="attribution"><blockquote class="quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="auto"><div class="quoted-text"><br><div>On Nov 29, 2017, at 12:40 PM, David Mertz <<a href="mailto:mertz@gnosis.cx" target="_blank">mertz@gnosis.cx</a>> wrote:<br><br></div><blockquote type="cite"><div><div dir="ltr">I like much of the thinking in Random's approach. But I still think None isn't quite special enough to warrant it's own syntax.<div><br></div><div>However, his '(or None: name.strip()[4:].upper())' makes me realize that what is being asked in all the '?(', '?.', '?[' syntax ideas is a kind of ternary expression. Except the ternary isn't based on whether a predicate holds, but rather on whether an exception occurs (AttributeError, KeyError, TypeError). And the fallback in the ternary is always None rather than being general.</div><div><br></div><div>I think we could generalize this to get something both more Pythonic and more flexible. E.g.:</div><div><br></div><div> val = name.strip()[4:].upper() except None</div><div><br></div><div>This would just be catching all errors, which is perhaps too broad. But it *would* allow a fallback other than None:</div><div><br></div><div> val = name.strip()[4:].upper() except -1<br></div><div><br></div><div>I think some syntax could be possible to only "catch" some exceptions and let others propagate. Maybe:</div><div><br></div><div> val = name.strip()[4:].upper() except (AttributeError, KeyError): -1</div><div><br></div><div>I don't really like throwing a colon in an expression though. Perhaps some other word or symbol could work instead. How does this read:</div><div><br></div><div><div> val = name.strip()[4:].upper() except -1 in (AttributeError, KeyError)</div></div><div><br></div><div>Where the 'in' clause at the end would be optional, and default to 'Exception'.</div><div><br></div><div>I'll note that what this idea DOES NOT get us is:</div><div><br></div><div> val = timeout ?? local_timeout ?? global_timeout</div><div><br></div><div>Those values that are "possibly None" don't raise exceptions, so they wouldn't apply to this syntax.</div></div></div></blockquote><div><br></div></div>See the rejected PEP 463 for Exception catching expressions. <div><br></div><div>Eric. </div><div><div class="elided-text"><br><blockquote type="cite"><div><div dir="ltr"><div><br></div><div>Yours, David...</div><div><br></div><div><br><div class="gmail_extra"><div class="gmail_quote">On Wed, Nov 29, 2017 at 9:03 AM, Random832 <span dir="ltr"><<a href="mailto:random832@fastmail.com" target="_blank">random832@fastmail.com</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"><span class="m_-1447184678332482178gmail-">On Tue, Nov 28, 2017, at 15:31, Raymond Hettinger wrote:<br>
><br>
> > I also cc python-dev to see if anybody here is strongly in favor or against this inclusion.<br>
><br>
> Put me down for a strong -1. The proposal would occasionally save a few<br>
> keystokes but comes at the expense of giving Python a more Perlish look<br>
> and a more arcane feel.<br>
><br>
> One of the things I like about Python is that I can walk non-programmers<br>
> through the code and explain what it does. The examples in PEP 505 look<br>
> like a step in the wrong direction. They don't "look like Python" and<br>
> make me feel like I have to decrypt the code to figure-out what it does.<br>
><br>
> timeout ?? local_timeout ?? global_timeout<br>
> 'foo' in (None ?? ['foo', 'bar'])<br>
> requested_quantity ?? default_quantity * price<br>
> name?.strip()[4:].upper()<br>
> user?.first_name.upper()<br>
<br>
</span>Since we're looking at different syntax for the ?? operator, I have a<br>
suggestion for the ?. operator - and related ?[] and ?() that appeared<br>
in some of the proposals. How about this approach?<br>
<br>
Something like (or None: ...) as a syntax block in which any operation<br>
[lexically within the expression, not within e.g. called functions, so<br>
it's different from simply catching AttributeError etc, even if that<br>
could be limited to only catching when the operand is None] on None that<br>
is not valid for None will yield None instead.<br>
<br>
This isn't *entirely* equivalent, but offers finer control.<br>
<br>
v = name?.strip()[4:].upper() under the old proposal would be more or<br>
less equivalent to:<br>
<br>
v = name.strip()[4:].upper() if name is not None else None<br>
<br>
Whereas, you could get the same result with:<br>
(or None: name.strip()[4:].upper())<br>
<br>
Though that would technically be equivalent to these steps:<br>
v = name.strip if name is not None else None<br>
v = v() if v """""<br>
v = v[4:] """""""<br>
v = v.upper """""""<br>
v = v() """""""<br>
<br>
The compiler could optimize this case since it knows none of the<br>
operations are valid on None. This has the advantage of being explicit<br>
about what scope the modified rules apply to, rather than simply<br>
implicitly being "to the end of the chain of dot/bracket/call operators"<br>
<br>
It could also be extended to apply, without any additional syntax, to<br>
binary operators (result is None if either operand is None) (or None: a<br>
+ b), for example, could return None if either a or b is none.<br>
<br>
[I think I proposed this before with the syntax ?(...), the (or None:<br>
...) is just an idea to make it look more like Python.]<br>
<div class="m_-1447184678332482178gmail-HOEnZb"><div class="m_-1447184678332482178gmail-h5">______________________________<wbr>_________________<br>
Python-Dev mailing list<br>
<a href="mailto:Python-Dev@python.org" target="_blank">Python-Dev@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/python-dev" rel="noreferrer" target="_blank">https://mail.python.org/mailma<wbr>n/listinfo/python-dev</a><br>
Unsubscribe: <a href="https://mail.python.org/mailman/options/python-dev/mertz%40gnosis.cx" rel="noreferrer" target="_blank">https://mail.python.org/mailma<wbr>n/options/python-dev/mertz%<wbr>40gnosis.cx</a><br>
</div></div></blockquote></div><br><br clear="all"><div><br></div>-- <br><div class="m_-1447184678332482178gmail_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></div></div>
</div></blockquote></div><blockquote type="cite"><div><div class="elided-text"><span>______________________________<wbr>_________________</span><br><span>Python-Dev mailing list</span><br><span><a href="mailto:Python-Dev@python.org" target="_blank">Python-Dev@python.org</a></span><br><span><a href="https://mail.python.org/mailman/listinfo/python-dev" target="_blank">https://mail.python.org/<wbr>mailman/listinfo/python-dev</a></span><br></div><span>Unsubscribe: <a href="https://mail.python.org/mailman/options/python-dev/eric%2Ba-python-dev%40trueblade.com" target="_blank">https://mail.python.org/<wbr>mailman/options/python-dev/<wbr>eric%2Ba-python-dev%<wbr>40trueblade.com</a></span><br></div></blockquote></div></div></blockquote></div><br></div></div></div>