[Python-ideas] Null coalescing operators
abarnert at yahoo.com
Sat Sep 19 05:30:48 CEST 2015
On Sep 18, 2015, at 19:06, Mark E. Haase <mehaase at 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
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 at python.org> wrote:
>> On Sep 18, 2015, at 18:00, Chris Angelico <rosuav at gmail.com> wrote:
>> >> On Sat, Sep 19, 2015 at 10:49 AM, Andrew Barnert <abarnert at 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
>> >>> spam?
>> >>> spam?.cheese
>> >>> spam?.cheese?
>> >>> spam?.cheese?()
>> 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 at python.org
>> > https://mail.python.org/mailman/listinfo/python-ideas
>> > Code of Conduct: http://python.org/psf/codeofconduct/
>> Python-ideas mailing list
>> Python-ideas at python.org
>> Code of Conduct: http://python.org/psf/codeofconduct/
> Mark E. Haase
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Python-ideas