为什么元组元素的 __cmp__ 在放入 Queue.PriorityQueue 时用于元组本身?

Why is the __cmp__ of a tuple element used for the tuple itself when put into a Queue.PriorityQueue?

如果我在 python 2.7.10

import Queue

class Item(object):
    def __init__(self):
        self.name = "bla"

    def __cmp__(a, b):
        return cmp(a.name, b.name)

e_queue = Queue.PriorityQueue()
e_queue.put((1,2))
item = Item()
e_queue.put((1, item))

第二个 put 失败

...
  File "/usr/lib/python2.7/Queue.py", line 136, in put
    self._put(item)
  File "/usr/lib/python2.7/Queue.py", line 225, in _put
    heappush(self.queue, item)
  File "tuple_nonsense.py", line 10, in __cmp__
    return cmp(a.name, b.name)

显然 Item__cmp__ 应用于包含它的 tupleThe Queue docs 甚至建议使用元组作为项目:

A typical pattern for entries is a tuple in the form: (priority_number, data).)

那么,这是什么问题?

在Python、when you compare sequences, the elements in them are compared on by one。因此,当比较元组 (1, 2)(1, item) 时,第一个元素是相同的。因此比较第二个元素以确定哪个较小。由于您有 2item,并且 item 已实现 __cmp__,它会调用该函数。

您可以用一个简单的 print 语句来确认这一点,例如

class Item(object):

    def __init__(self):
        self.name = "bla"

    def __cmp__(a, b):
        print a, b
        return cmp(a.name, b.name)

此处 a 将是 item 对象,b 将是 2。由于 2 没有 name 属性,因此失败并显示

AttributeError: 'int' object has no attribute 'name'