Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Boolean expression performs redundant bool check #3567

Open
dimpolo opened this issue Mar 11, 2022 · 0 comments
Open

Boolean expression performs redundant bool check #3567

dimpolo opened this issue Mar 11, 2022 · 0 comments

Comments

@dimpolo
Copy link

@dimpolo dimpolo commented Mar 11, 2022

n = 0

class Test:
    def __bool__(self):
        global n
        n += 1
        return False

Test() and False or False
assert n == 1

CPython optimizes this check in:
https://github.com/python/cpython/blob/54ab9ad312ea53db40e31712454272e1d4c0315f/Python/compile.c#L8708-L8721

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant