Search code examples
pythonqueuepriority-queue

Why is the bool evaluation of an empty PriorityQueue True?


Why doesn't an empty PriorityQueue evaluate to False like other iterables in Python?

>>> from queue import PriorityQueue
>>> q1 = PriorityQueue()
>>> bool(q1)
True
>>> q1.qsize()
0

Solution

  • As you can see from the source code, the PriorityQueue class doesn't implement __len__ or __bool__, and the default is that if an object is present it's truthy:

    If a class defines neither __len__() nor __bool__(), all its instances are considered true.