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

Random832 random832 at fastmail.com
Thu Oct 1 22:36:53 CEST 2015


On Thu, Oct 1, 2015, at 16:07, MRAB wrote:
> If a is falsy, it short-circuits "a and b".
> 
> The parenthesised expression returns a falsy result.
> 
> That falsy result then short-circuits "(a and b) and c".
> 
> It happens to show the same behaviour as "a and b and c" and can be
> optimised to that.

Er... that's what a and b and c *is*. The 'and' operator is a
left-associative binary operator.

((a and b) and c) is literally the same AST as (a and b and c). Being
optimized to "short-circuit the whole thing" is an optimization for both
of them. The naive way you describe of evaluating each one in turn is
also the same for both of them. They are in fact both being optimized to
(a and (b and c)) [rather, to the same byte code that a 'naive'
implementation would still generate for that expression].


More information about the Python-ideas mailing list