This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author josh.r
Recipients Mark.Shannon, Mohamed_Atef, ethan.furman, gregory.p.smith, gvanrossum, josh.r, pablogsal, serhiy.storchaka, stestagg
Date 2021-01-13.05:07:09
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1610514430.14.0.689694429657.issue42899@roundup.psfhosted.org>
In-reply-to
Content
Gregory: Even in a low-level compiled language (say, C++), pretty sure the compiler can't automatically optimize out:

    if (x) { }

unless it has sure knowledge of the implementation of operator bool; if operator bool's implementation isn't in the header file, and link time optimization isn't involved, it has to call it to ensure any side-effects it might have are invoked.

It can only bypass the call if it knows the implementation of operator bool and can verify it has no observable side-effects (as-if rule). There are exceptions to the as-if rule for optimizations in special cases (copy elision), but I'm pretty sure operator bool isn't one of them; if the optimizer doesn't know the implementation of operator bool, it must call it just in case it does something weird but critical to the program logic.

Point is, I agree that:

if x:
     pass

must evaluate non-constant-literal x for truthiness, no matter how silly that seems (not a huge loss, given very little code should ever actually do that).
History
Date User Action Args
2021-01-13 05:07:10josh.rsetrecipients: + josh.r, gvanrossum, gregory.p.smith, ethan.furman, Mark.Shannon, serhiy.storchaka, Mohamed_Atef, pablogsal, stestagg
2021-01-13 05:07:10josh.rsetmessageid: <1610514430.14.0.689694429657.issue42899@roundup.psfhosted.org>
2021-01-13 05:07:10josh.rlinkissue42899 messages
2021-01-13 05:07:09josh.rcreate