[Python-ideas] Null coalescing operator

MRAB python at mrabarnett.plus.com
Wed Nov 2 14:34:14 EDT 2016


On 2016-11-02 16:17, Nick Coghlan wrote:
[snip]
> Yeah, and so far the protocol based alternative I'm working on hasn't
> been any less headache-inducing (Mark has been reviewing some early
> iterations and had to draw a diagram to try to follow the proposed
> control flow).
>
> I think I have a way to simplify that idea further though, and if that
> works out I should have something I'm willing to share with a wider
> audience.
>
> The gist is that rather than writing the bare:
>
>     target = expr1 ?? expr2 ?? expr3
>
> You'd instead write None-coalescing as:
>
>     target = exists(expr1) ?? exists(expr2) ?? expr3
>
> and None-propagating as:
>
>     target = missing(expr1) ?? missing(expr2) ?? expr3
>
> with ?? being a protocol-driven short-circuiting binary operator
> controlled by the left operand rather than defining any particular
> semantics of its own.
>
> The "obj?." and "obj?[]" would then be shorthand for particular uses
> of "missing(obj) ?? ..." that avoid duplicate evaluation of the left
> operand (as well as bypassing the overhead of actually creating a
> "missing" instance).
>
How about borrowing from C:

     target = expr1 || expr2 || expr3
     target = expr1 && expr2 && expr3

except that only None would be considered falsey?

Or would that be confusing?



More information about the Python-ideas mailing list