<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>1</div><div><br></div><div>This works if NoneQuestion overrides __nonzero__ to return False.</div><div><br></div><div>>>> 0? or 1<br></div><div>0</div><div><br></div><div>This doesn't work, because 0? returns 0, and "0 or 1" is 1. </div><div><br></div><div>We could try this instead, if NoneQuestion overrides __or__:</div><div><br></div><div>>>> 0? | 1</div><div>0</div><div>>>> 0 ?| 1</div><div>0</div><div><br></div><div>This looks a little ugly, and it would be nice (as MRAB pointed out) if null coalescing short circuited.</div><div><br></div><div>>>> None? or None?</div><div><br></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><br></div><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><br></div><div><br></div></div><div class="gmail_extra"><br><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>