<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8" /></head><body dir="auto">This is likely going to get shot down quickly...<br>
<br>
I know CoffeeScript is not regarded too well in this community (well, at least based on Guido's remarks on parsing it), but what if x? was shorthand for x is None? In CS, it's called the existential operator.<br><br><div class="gmail_quote">On September 18, 2015 10:30:48 PM CDT, Andrew Barnert via Python-ideas <python-ideas@python.org> wrote:<blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
<div>On Sep 18, 2015, at 19:06, Mark E. Haase <<a href="mailto:mehaase@gmail.com">mehaase@gmail.com</a>> wrote:</div><div><br /></div><blockquote type="cite"><div><div dir="ltr">Andrew, I really like that idea. Turning back to the null coalescing operator (spelled ?? in other languages), how do you think that fits in? <div><br /></div><div>Consider this syntax:<div><div><br /></div><div>>>> None? or 1</div></div></div></div></div></blockquote><div><br /></div><div>I don't think there's any easy way to make "spam? or 1" work any better than "spam or 1" already does, partly for the reasons you give below, but also because it doesn't seem to fit the design in any obvious way.</div><div><br /></div><div>I guess that means postix ? doesn't quite magically solve everything...</div><br /><blockquote type="cite"><div><div dir="ltr"><div><div><div>This also doesn't work quite right. If both operands are None, we want the expression to evaluate to None, not NoneQuestion.
<b>Should null coalescing be a separate operator? And if so, are "?" and "??" too similar?</b></div></div></div></div></div></blockquote><div><br /></div><div>As MRAB pointed out, there seem to be good reasons to let spam?? mean the same thing as spam? (and that follows automatically from the simplest possible definition, the one I gave above). So I think "spam ?? eggs" is ambiguous between the postfix operator and the infix operator without lookahead, at least to a human, and possibly to the compiler as well.</div><div><br /></div><div>I suppose ?: as in ColdFusion might work, but (a) ewwww, (b) it regularly confuses novices to CF, and (c) it's impossible to search for, because ?: no matter how you quote it gets you the C ternary operator....</div><br /><blockquote type="cite"><div><div dir="ltr"><div>Can anybody think of realistic use cases for overriding a magic method for the "?" operator? I would like to include such use cases in a PEP. One possible use case: being able to
coalesce empty strings.</div><div><br /></div><div>>>> s1 = MyString('')</div><div>>>> s2 = MyString('foobar')</div><div>>>> s1? or s2</div><div>MyString('foobar')</div></div></div></blockquote><div><br /></div><div>This seems like a bad idea. Empty strings are already falsey. If you want this behavior, why not just use "s1 or s2", which already works, and for obvious reasons?</div><br /><blockquote type="cite"><div><div class="gmail_extra"><div class="gmail_quote">On Fri, Sep 18, 2015 at 9:10 PM, Andrew Barnert via Python-ideas <span dir="ltr"><<a href="mailto:python-ideas@python.org" target="_blank">python-ideas@python.org</a>></span> wrote:<br /><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span class="">On Sep 18, 2015, at 18:00, Chris Angelico <<a href="mailto:rosuav@gmail.com">rosuav@gmail.com</a>> wrote:<br />
><br />
>> On Sat, Sep 19, 2015 at 10:49 AM, Andrew Barnert <<a href="mailto:abarnert@yahoo.com">abarnert@yahoo.com</a>> wrote:<br />
>> Obviously "spam?" returns something with a __getattr__ method that just passes through to spam.__getattr__, except that on NoneType it returns something with a __getattr__ that always returns None. That solves the eggs case.<br />
>><br />
>> Next, "spam?.cheese?" returns something with a __call__ method that just passed through to spam?.cheese.__call__, except that on NoneType it returns something with a __call__ that always returns None. That solves the cheese case.<br />
><br />
> Hang on, how do you do this? How does the operator know the difference<br />
> between "spam?", which for None has to have __getattr__ return None,<br />
> and "spam?.cheese?" that returns (lambda: None)?<br />
<br />
</span>>>> spam<br />
None<br />
>>> spam?<br />
NoneQuestion<br />
>>> spam?.cheese<br />
None<br />
>>> spam?.cheese?<br />
NoneQuestion<br />
>>> spam?.cheese?()<br />
None<br />
<br />
All you need to make this work is:<br />
<br />
* "spam?" returns NoneQuestion if spam is None else spam<br />
* NoneQuestion.__getattr__(self, *args, **kw) returns None.<br />
* NoneQuestion.__call__(self, *args, **kw) returns None.<br />
<br />
Optionally, you can add more None-returning methods to NoneQuestion. Also, whether NoneQuestion is a singleton, has an accessible name, etc. are all bikesheddable.<br />
<br />
I think it's obvious what happens is "spam" is not None and "spam.cheese" is, or of both are None, but if not, I can work them through as well.<br />
<div class="HOEnZb"><div class="h5"><br />
<br />
> ChrisA<br />
> _______________________________________________<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/mailman/listinfo/python-ideas</a><br />
> Code of Conduct: <a href="http://python.org/psf/codeofconduct/" rel="noreferrer" target="_blank">http://python.org/psf/codeofconduct/</a><br />
_______________________________________________<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/mailman/listinfo/python-ideas</a><br />
Code of Conduct: <a href="http://python.org/psf/codeofconduct/" rel="noreferrer" target="_blank">http://python.org/psf/codeofconduct/</a><br />
</div></div></blockquote></div><br /><br clear="all" /><div><br /></div>-- <br /><div class="gmail_signature"><div dir="ltr">Mark E. Haase<br /><a style="color:rgb(17,85,204)">202-815-0201</a><br /></div></div>
</div>
</div></blockquote><p style="margin-top: 2.5em; margin-bottom: 1em; border-bottom: 1px solid #000"></p><pre class="k9mail"><hr /><br />Python-ideas mailing list<br />Python-ideas@python.org<br /><a href="https://mail.python.org/mailman/listinfo/python-ideas">https://mail.python.org/mailman/listinfo/python-ideas</a><br />Code of Conduct: <a href="http://python.org/psf/codeofconduct/">http://python.org/psf/codeofconduct/</a></pre></blockquote></div><br>
-- <br>
Sent from my Nexus 5 with K-9 Mail. Please excuse my brevity.</body></html>