[Python-ideas] Null coalescing operator

Guido van Rossum guido at python.org
Sat Sep 10 19:43:13 EDT 2016


On Sat, Sep 10, 2016 at 4:27 PM, Chris Angelico <rosuav at gmail.com> wrote:
> On Sun, Sep 11, 2016 at 9:10 AM, Guido van Rossum <guido at python.org> wrote:
>> So you're offering `NoneCoalesce(x).bar` as less-ugly alternative to
>> `x?.bar`... Color me unconvinced.
>
> As a syntactic form? Not interested. But what if it's the underlying
> implementation? We have "yield from X" as a tidy syntax for roughly a
> page of equivalent code. We could have "x?.bar" as syntactic sugar for
> "NoneCoalesce(x).bar".

PEP 505 has an option for a way to customize the coalescing operation
(https://www.python.org/dev/peps/pep-0505/#generalized-coalescing).
Though I think I'd rather not do that.

But it just occurs to me that the implementation given by David Mertz
is not what I'd expect: it seems that `NoneCoalesce([]).flup` would
catch the AttributeError (there' no `[].flup`) and return
NoneCoalesce(None), whereas I would expect `?.` to only return None
when the LHS is None, not when some other not-None object doesn't have
the requested attribute. (And the "pile of poo" operator in PEP 505
agrees with me.)

-- 
--Guido van Rossum (python.org/~guido)


More information about the Python-ideas mailing list