Why does the expression 0 < 0 == 0 return False in Python?

前端 未结 9 1551
误落风尘
误落风尘 2020-11-22 01:48

Looking into Queue.py in Python 2.6, I found this construct that I found a bit strange:

def full(self):
    \"\"\"Return True if the queue is full, False oth         


        
9条回答
  •  不知归路
    2020-11-22 02:13

    The strange behavior your experiencing comes from pythons ability to chain conditions. Since it finds 0 is not less than 0, it decides the entire expression evaluates to false. As soon as you break this apart into seperate conditions, you're changing the functionality. It initially is essentially testing that a < b && b == c for your original statement of a < b == c.

    Another example:

    >>> 1 < 5 < 3
    False
    
    >>> (1 < 5) < 3
    True
    

提交回复
热议问题