[Python-ideas] Null coalescing operators

Guido van Rossum guido at python.org
Sat Sep 19 06:21:56 CEST 2015


On Fri, Sep 18, 2015 at 8:41 PM, Steven D'Aprano <steve at pearwood.info>
wrote:

> It's a funny thing, I'm usually not a huge fan of symbols outside of
> maths operators, and I strongly dislike the C ? ternary operator, but
> this one feels really natural to me. I didn't have even the most
> momentary "if you want Perl, you know where to find it" thought.
>

I do, but at least the '?' is part of an operator, not part of the name (as
it is in Ruby?).

I really, really, really don't like how it looks, but here's one thing: the
discussion can be cut short and focus almost entirely on whether this is
worth making Python uglier (and whether it's even ugly :-). The semantics
are crystal clear and it's obvious that the way it should work is by making
"?.", ?(" and "?[" new operators or operator pairs -- the "?" should not be
a unary postfix operator but a symbol that combines with certain other
symbols.

Let me propose a (hyper?)generalization: it could be combined with any
binary operation, e.g. "a?+b" would mean "None if a is None else a+b".
Sadly (as hypergeneralizations tend to do?) this also leads to a negative
observation: what if I wanted to write "None if b is None else a+b"? (And
don't be funny and say I should swap a and b -- they could be strings.)
Similar for what if you wanted to do this with a unary operator, e.g. None
if x is None else -x. Maybe we could write "a+?b" and "-?x"? But I don't
think the use cases warrant these much.

Finally, let's give it a proper name -- let's call it the uptalk operator.

-- 
--Guido van Rossum (python.org/~guido)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20150918/9e977982/attachment-0001.html>


More information about the Python-ideas mailing list