Using `or?` as the null coalescing operator
I really like PEP 0505. The only thing that does not convince me is the `??` operator. I would like to know what you think of an alternative like `or?`: a_list = some_list or? [] a_dict = some_dict or? {} The rationale behind is to let `or` do its job with “truthy” values, while `or?` would require non-None values. The rest of the PEP looks good to me. I apologise in advance if this was already proposed and I missed it. Regards, Alessio
On 25 September 2015 at 09:07, Alessio Bogon
I really like PEP 0505. The only thing that does not convince me is the `??` operator. I would like to know what you think of an alternative like `or?`:
a_list = some_list or? [] a_dict = some_dict or? {}
The rationale behind is to let `or` do its job with “truthy” values, while `or?` would require non-None values. The rest of the PEP looks good to me.
I apologise in advance if this was already proposed and I missed it.
It hasn't been suggested that I recall, and yes, I also prefer it to the doubled ?? spelling. One concrete advantage is that it helps convey that this is a short-circuiting control flow operator like 'and' and 'or' rather than a normal binary operator that always evaluates both operands. Regards, Nick. -- Nick Coghlan | ncoghlan@gmail.com | Brisbane, Australia
On Mon, Sep 28, 2015 at 10:13 AM, Nick Coghlan
On 25 September 2015 at 09:07, Alessio Bogon
wrote: I really like PEP 0505. The only thing that does not convince me is the `??` operator. I would like to know what you think of an alternative like `or?`:
a_list = some_list or? [] a_dict = some_dict or? {}
And have the following syntax options been considered? a_list = some_list else [] a_list = some_list or [] if None -- Koos
On September 28, 2015 5:50:15 AM CDT, Koos Zevenhoven
On 25 September 2015 at 09:07, Alessio Bogon
wrote: I really like PEP 0505. The only thing that does not convince me is
On Mon, Sep 28, 2015 at 10:13 AM, Nick Coghlan
wrote: the `??` operator. I would like to know what you think of an alternative like `or?`: a_list = some_list or? [] a_dict = some_dict or? {}
And have the following syntax options been considered?
a_list = some_list else []
This one's ambiguous. How would you parse: x if a else b else c As: x if (a else b) else c Or: x if a else (b else c)
a_list = some_list or [] if None
-- Koos _______________________________________________ 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.
On 28 September 2015 at 20:50, Koos Zevenhoven
On Mon, Sep 28, 2015 at 10:13 AM, Nick Coghlan
wrote: On 25 September 2015 at 09:07, Alessio Bogon
wrote: I really like PEP 0505. The only thing that does not convince me is the `??` operator. I would like to know what you think of an alternative like `or?`:
a_list = some_list or? [] a_dict = some_dict or? {}
And have the following syntax options been considered?
a_list = some_list else []
In addition to the syntactic ambiguity Ryan notes, there's no hint here that we're using "some_list is not None" as the condition rather than "bool(some_list)"
a_list = some_list or [] if None
This one isn't supportable at the language grammar level - by the time we get to the "if" token, the parser will have already interpreted the first part as "some_list or []". Cheers, Nick. -- Nick Coghlan | ncoghlan@gmail.com | Brisbane, Australia
participants (4)
-
Alessio Bogon
-
Koos Zevenhoven
-
Nick Coghlan
-
Ryan Gonzalez