Skip to content

Boolean expression performs redundant bool check #3567

@dimpolo

Description

@dimpolo
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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions