为什么空 PriorityQueue 的 bool 评估为真?
Why is the bool evaluation of an empty PriorityQueue True?
为什么空的 PriorityQueue 不像 Python 中的其他可迭代对象那样计算为 False
?
>>> from queue import PriorityQueue
>>> q1 = PriorityQueue()
>>> bool(q1)
True
>>> q1.qsize()
0
正如您从 the source code, the PriorityQueue
class doesn't implement __len__
or __bool__
中看到的那样,默认情况下,如果存在对象,则它是真实的:
If a class defines neither __len__()
nor __bool__()
, all its
instances are considered true.
为什么空的 PriorityQueue 不像 Python 中的其他可迭代对象那样计算为 False
?
>>> from queue import PriorityQueue
>>> q1 = PriorityQueue()
>>> bool(q1)
True
>>> q1.qsize()
0
正如您从 the source code, the PriorityQueue
class doesn't implement __len__
or __bool__
中看到的那样,默认情况下,如果存在对象,则它是真实的:
If a class defines neither
__len__()
nor__bool__()
, all its instances are considered true.