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

前端 未结 9 1564
误落风尘
误落风尘 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:08

    Here it is, in all its glory.

    >>> class showme(object):
    ...   def __init__(self, name, value):
    ...     self.name, self.value = name, value
    ...   def __repr__(self):
    ...     return "" % (self.name, self.value)
    ...   def __cmp__(self, other):
    ...     print "cmp(%r, %r)" % (self, other)
    ...     if type(other) == showme:
    ...       return cmp(self.value, other.value)
    ...     else:
    ...       return cmp(self.value, other)
    ... 
    >>> showme(1,0) < showme(2,0) == showme(3,0)
    cmp(, )
    False
    >>> (showme(1,0) < showme(2,0)) == showme(3,0)
    cmp(, )
    cmp(, False)
    True
    >>> showme(1,0) < (showme(2,0) == showme(3,0))
    cmp(, )
    cmp(, True)
    True
    >>> 
    

提交回复
热议问题