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

Mark Shannon report at bugs.python.org
Tue Jan 12 11:36:23 EST 2021


Mark Shannon <mark at hotpy.org> added the comment:

It's clearer if you rewrite

if a and b:
    ...

as

tmp = a and b
if tmp:
    ...

if a is falsey then bool(a) gets called in `tmp = a and b` and `a` is assigned to `tmp`. Then in `if tmp`, bool(a) is called again.

I agree with you about it not being an optimization if it changes the semantics. But only within agreed semantics.
Optimizations are allow to make fewer calls to __hash__() in a dictionary, or change race conditions, because those are outside of the language specification.

----------

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


More information about the Python-bugs-list mailing list