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

前端 未结 7 2118
谎友^
谎友^ 2020-12-08 06:26

I did several Boolean Comparisons:

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

It sounds like

相关标签:
7条回答
  • 2020-12-08 06:38

    Yes. There are guaranteed to be exactly two bools, True and False:

    Class bool cannot be subclassed further. Its only instances are False and True.

    That means if you know both operands are bool, == and is are equivalent. However, as detly notes, there's usually no reason to use either in this case.

    0 讨论(0)
  • 2020-12-08 06:43

    Watch out for what else you may be comparing.

    >>> 1 == True
    True
    >>> 1 is True
    False
    

    True and False will have stable object ids for their lifetime in your python instance.

    >>> id(True)
    4296106928
    >>> id(True)
    4296106928
    

    is compares the id of an object

    EDIT: adding or

    Since OP is using or in question it may be worth pointing this out.

    or that evaluates True: returns the first 'True' object.

    >>> 1 or True
    1
    >>> 'a' or True
    'a'
    >>> True or 1
    True
    

    or that evaluates False: returns the last 'False' object

    >>> False or ''
    ''
    >>> '' or False
    False
    

    and that evaluates to True: returns the last 'True' object

    >>> True and 1
    1
    >>> 1 and True
    True
    

    and that evaluates to False: returns the first 'False' object

    >>> '' and False
    ''
    >>> False and ''
    False
    

    This is an important python idiom and it allows concise and compact code for dealing with boolean logic over regular python objects.

    >>> bool([])
    False
    >>> bool([0])
    True
    >>> bool({})
    False
    >>> bool({False: False})
    True
    >>> bool(0)
    False
    >>> bool(-1)
    True
    >>> bool('False')
    True
    >>> bool('')
    False
    

    Basically 'empty' objects are False, 'non empty' are True.

    Combining this with @detly's and the other answers should provide some insight into how to use if and bools in python.

    0 讨论(0)
  • 2020-12-08 06:44

    == and is are both comparison operators, which would return a boolean value - True or False. True has a numeric value of 1 and False has a numeric value of 0.

    The operator == compare the values of two objects and objects compared are most often are the same types (int vs int, float vs float), If you compare objects of different types, then they are unequal. The operator is tests for object identity, 'x is y' is true if both x and y have the same id. That is, they are same objects.

    So, when you are comparing if you comparing the return values of same type, use == and if you are comparing if two objects are same (be it boolean or anything else), you can use is.

    42 is 42 is True and is same as 42 == 42.

    0 讨论(0)
  • 2020-12-08 06:53

    The == operator tests for equality The is keyword tests for object identity. Whether we are talking about the same object. Note, that more variables may refer to the same object.

    0 讨论(0)
  • 2020-12-08 06:55

    It seems that all answers deal with True and False as defined after an interpreter startup. Before booleans became part of Python they were often defined as part of a program. Even now (Python 2.6.6) they are only names that can be pointed to different objects:

    >>> True = 1
    >>> (2 > 1)
    True
    >>> (2 > 1) == True
    True
    >>> (2 > 1) is True
    False
    

    If you have to deal with older software, be aware of that.

    0 讨论(0)
  • 2020-12-08 07:02

    Another reason to compare values using == is that both None and False are “falsy” values. And sometimes it’s useful to use None to mark a value as “not defined” or “no value” while considering True and False values to work with:

    def some_function(val = None):
        """This function does an awesome thing."""
        if val is None:
            # Values was not defined.
        elif val == False:
            # User passed boolean value.
        elif val == True:
            # User passed boolean value.
        else:
           # Quack quack.
    

    Somewhat related question: Python != operation vs “is not”.

    0 讨论(0)
提交回复
热议问题