This is likely going to get shot down quickly...

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.

On September 18, 2015 10:30:48 PM CDT, Andrew Barnert via Python-ideas <python-ideas@python.org> wrote:
On Sep 18, 2015, at 19:06, Mark E. Haase <mehaase@gmail.com> wrote:

Andrew, I really like that idea. Turning back to the null coalescing operator (spelled ?? in other languages), how do you think that fits in? 

Consider this syntax:

>>> None? or 1

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.

I guess that means postix ? doesn't quite magically solve everything...

This also doesn't work quite right. If both operands are None, we want the expression to evaluate to None, not NoneQuestion. Should null coalescing be a separate operator? And if so, are "?" and "??" too similar?

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.

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....

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.

>>> s1 = MyString('')
>>> s2 = MyString('foobar')
>>> s1? or s2
MyString('foobar')

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?

On Fri, Sep 18, 2015 at 9:10 PM, Andrew Barnert via Python-ideas <python-ideas@python.org> wrote:
On Sep 18, 2015, at 18:00, Chris Angelico <rosuav@gmail.com> wrote:
>
>> On Sat, Sep 19, 2015 at 10:49 AM, Andrew Barnert <abarnert@yahoo.com> wrote:
>> 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.
>>
>> 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.
>
> Hang on, how do you do this? How does the operator know the difference
> between "spam?", which for None has to have __getattr__ return None,
> and "spam?.cheese?" that returns (lambda: None)?

>>> spam
None
>>> spam?
NoneQuestion
>>> spam?.cheese
None
>>> spam?.cheese?
NoneQuestion
>>> spam?.cheese?()
None

All you need to make this work is:

* "spam?" returns NoneQuestion if spam is None else spam
* NoneQuestion.__getattr__(self, *args, **kw) returns None.
* NoneQuestion.__call__(self, *args, **kw) returns None.

Optionally, you can add more None-returning methods to NoneQuestion. Also, whether NoneQuestion is a singleton, has an accessible name, etc. are all bikesheddable.

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.


> ChrisA
> _______________________________________________
> Python-ideas mailing list
> Python-ideas@python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
_______________________________________________
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/



--
Mark E. Haase
202-815-0201



Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/

--
Sent from my Nexus 5 with K-9 Mail. Please excuse my brevity.