The built-in function len()
(https://docs.python.org/3/library/functions.html#len) returns "the length (the number of items) of an object", but this is not implemented for queue.Queue
(https://docs.python.org/3/library/queue.html).
Instead, queue.Queue
has a qsize()
method which returns the approximate size of a queue, when it clearly has a length; you can specify the maximum length of a Queue
in the constructor. The similar collections.deque
does work with len
.
What are the reasons for not using the common len()
for queue.Queue
? Or: What would be the problems if qsize
were instead named __len__
to enable the len()
function?
len
to return an accurate value, not an approximate one. And that it should be fast. To implement an accurate__len__
the Queue would have to be temporarily locked to prevent additions & removals, which would impact performance.