你这个例子只是碰巧了。
优先队列是基于heapq(最小堆)的。了解一下最小堆的实现就明白了。
python中文文档里的8.5.3原理讲解http://python.usyiyi.cn/trans...
下面反例:
In [1]: from queue import PriorityQueue
In [2]: number = PriorityQueue(10)
In [3]: for i in range(8):
...: number.put(i)
...:
# 堆中的数据是存放在list中的,所以可以看到number.queue类型是list。
In [4]: number.queue?
Type: list
String form: [0, 1, 2, 3, 4, 5, 6, 7]
Length: 8
Docstring:
list() -> new empty list
list(iterable) -> new list initialized from iterable's items
In [5]: number.queue
Out[5]: [0, 1, 2, 3, 4, 5, 6, 7]
In [6]: number.put(-3)
In [7]: number.queue
Out[7]: [-3, 0, 2, 1, 4, 5, 6, 7, 3]