Why is the comparison with True / False using the is is bad operator?

PEP8 states that the check Noneshould be performed with is None. The same is true for single individuals. But PEP8 also states that the equality of True and False should not be performed using the operator, iseven worse than ==:

Yes:   if greeting:
No:    if greeting == True:
Worse: if greeting is True:

I understand that the first of them is the most beautiful and the most pythonic, but sometimes I am in a situation where I want to clearly show that I am comparing with Trueor False. How isworse than ==in this situation?

+4
source share
2 answers

== ( ), is .

, True, is True. bool, :

class MyBool:
    def __init__(self, is_true):
        self.is_true = bool(is_true)

    def __eq__(self, other):
        return self.is_true == bool(other)

    def __bool__(self):
        return self.is_true

, a = MyBool(True) if a:, bool(a) a == True . a is True False.

+4

is "" , . , , . True singleton .

:

1 == True 

:

1 is True
+3

Source: https://habr.com/ru/post/1625525/


All Articles