[Python-ideas] Fwd: Null coalescing operator
David Mertz
mertz at gnosis.cx
Sun Sep 11 21:22:23 EDT 2016
On Sun, Sep 11, 2016 at 6:11 PM, Guido van Rossum <guido at python.org> wrote:
> On Sun, Sep 11, 2016 at 6:00 PM, David Mertz <mertz at gnosis.cx> wrote:
> > None if a is None else a.foo
>
> This is the crux of the matter to me. It's just too verbose, and the
> `if` and `else` keywords are lost in the noise of all the other words
> on the line. Plus the big win when it applies) is that if `a` is in
> fact something more complex, like `f(a)`, repeating it twice sounds
> like a performance penalty, and that's where `f(a)?.foo` really
> shines.
>
The non-repetition is certain a plus, I readily confess. It's not only
performance even; `f()` might not be a pure function. Silly example:
>>> def f(a):
.... if random() < .01:
.... return None
.... class V: pass
.... v = V()
.... v.foo = random()*a
.... return v
--
Keeping medicines from the bloodstreams of the sick; food
from the bellies of the hungry; books from the hands of the
uneducated; technology from the underdeveloped; and putting
advocates of freedom in prisons. Intellectual property is
to the 21st century what the slave trade was to the 16th.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20160911/a3216f92/attachment.html>
More information about the Python-ideas
mailing list