[Python-ideas] PEP 505 (None coalescing operators) thoughts

Carl Meyer carl at oddbird.net
Tue Sep 29 19:57:24 CEST 2015

Hi Barry,

On 09/29/2015 11:35 AM, Barry Warsaw wrote:
> On Sep 28, 2015, at 03:04 PM, Carl Meyer wrote:
>> But even if they are rejected, I think a simple `??` or `or?` (or
>> however it's spelled) operator to reduce the repetition of "x if x is
>> not None else y" is worth consideration on its own merits. This operator
>> is entirely unambiguous, and I think would be useful and frequently
>> used, whether or not ?. and ?[ are added along with it.
> But why is it an improvement?  The ternary operator is entirely obvious and
> readable, and at least in my experience, is rare enough that the repetition
> doesn't hurt my fingers that much.  It seems like such a radical, ugly new
> syntax unjustified by the frequency of use and readability improvement.

I find the repetition irritating enough that I'm tempted to use 'or'
instead, even when I know it's not technically the semantics I want. (In
most cases, the difference probably doesn't matter, and when it actually
does, I probably know that and write out the full ternary.) And I find
plenty of other code using `or` when it ought to be using a ternary with
`is None` (but again, most of the time in practice it's fine.) Most of
this code is in defaults-handling; there've been plenty of examples in
the thread. I find the explicit if-block painful if there's more than
one argument with a None-default to be handled; YMMV.

I agree that I don't love any of the syntax suggestions so far, and
without a less ugly syntax, it's probably dead.

If it was in the language, I'd use it, but I don't feel strongly about it.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 836 bytes
Desc: OpenPGP digital signature
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20150929/ae175671/attachment.sig>

More information about the Python-ideas mailing list