[issue42899] Is it legal to eliminate tests of a value, when that test has no effect on control flow?

Steve Stagg report at bugs.python.org
Tue Jan 12 06:39:27 EST 2021


Steve Stagg <stestagg at gmail.com> added the comment:

To be super pedantic, as per my understanding of:

"6.11 ... The expression x and y first evaluates x; if x is false, its value is returned; otherwise, y is evaluated and the resulting value is returned."

The only corner that was previously cut is that in this statement:

if a and b:
    ...


The evalution should be roughly equivalent to:

bool(a) if bool(a) else bool(b) # <- where bool(b) is never called

instead it's more like:

_x if _x := bool(a) else bool(b) # <- where bool(b) is never called

so, the runtime is eliding a repeated call to bool(a).

This obviously causes problems if bool(a) has per-call side-effects, but this seems to me like a reasonable corner to cut.

Totally eliding the if clause feels to me (subjectively) like a much more risky proposition, and perhaps one that should be documented if kept in?

----------

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue42899>
_______________________________________


More information about the Python-bugs-list mailing list