PriorityQueue自定义类中的TypeError

时间:2016-02-12 08:21:17

标签: python class python-3.x queue

from queue import PriorityQueue

class NewQueue(PriorityQueue):
    def __init__(self, maxsize=None):
        PriorityQueue.__init__(self, maxsize)

    def put(self, item, block=True, timeout=None):
        PriorityQueue.put(self, item)


queue = NewQueue()

queue.put('abc')

我试图创建一个自定义类的PriorityQueue,但我得到了 以下错误:

    PriorityQueue.put(self, item)
TypeError: unorderable types: NoneType() > int()

为什么会发生这种情况,我怎样才能使这个自定义类工作?

2 个答案:

答案 0 :(得分:2)

问题不在于对queue.put的调用,而在于您将None作为maxsize传递。如果您想拥有无限制的队列,请使用0作为maxsize

错误发生在put,因为它检查maxsize是否大于0 None将会失败。

所以只需更改__init__

的定义即可
class NewQueue(PriorityQueue):
    def __init__(self, maxsize=0):
        ...

答案 1 :(得分:1)

您正在尝试使用maxsize=None创建一个队列。尝试使用

计算对象
queue = NewQueue(0)

或更改您的__init__签名:

def __init__(self, maxsize=0):