[Python] Is it safe to replace '==' with 'is' to compare Boolean-values

Discussão em 'Python' iniciado por Stack, Maio 2, 2025.

  1. Stack

    Stack Membro Participativo

    I did several Boolean Comparisons:

    >>> (True or False) is True
    True
    >>> (True or False) == True
    True


    It sounds like == and is are interchangeable for Boolean-values.

    Sometimes it's more clear to use is

    I want to know that:

    Are True and False pre-allocated in python?

    Is bool(var) always return the same True(or False) with the pre-allocated True(or False)?

    Is it safe to replace == with is to compare Boolean-values?


    It's not about Best-Practice.

    I just want to know the Truth.

    Continue reading...

Compartilhe esta Página