Usage of the “==” operator for three objects

前端 未结 1 871
粉色の甜心
粉色の甜心 2021-01-12 13:07

Is there any computational difference between these two methods of checking equality between three objects?

I have two variables: x and y.

相关标签:
1条回答
  • 2021-01-12 13:40

    Python has chained comparisons, so these two forms are equivalent:

    x == y == z
    x == y and y == z
    

    except that in the first, y is only evaluated once.

    This means you can also write:

    0 < x < 10
    10 >= z >= 2
    

    etc. You can also write confusing things like:

    a < b == c is d   # Don't  do this
    

    Beginners sometimes get tripped up on this:

    a < 100 is True   # Definitely don't do this!
    

    which will always be false since it is the same as:

    a < 100 and 100 is True   # Now we see the violence inherent in the system!
    
    0 讨论(0)
提交回复
热议问题