【问题标题】:TypeError in PriorityQueue custom classPriorityQueue 自定义类中的 TypeError
【发布时间】:2016-05-23 07:39:53
【问题描述】:
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()

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

【问题讨论】:

标签: python class python-3.x queue


【解决方案1】:

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

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

所以只需更改__init__的定义

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

【讨论】:

    【解决方案2】:

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

    来实例化您的对象
    queue = NewQueue(0)
    

    或更改您的__init__ 签名:

    def __init__(self, maxsize=0):
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-06-13
      • 2022-08-03
      • 1970-01-01
      • 2018-04-15
      • 2021-02-11
      • 2020-04-24
      • 2020-09-12
      相关资源
      最近更新 更多