how to implement a queue-like container with sort function

Ned Batchelder ned at nedbatchelder.com
Mon Dec 2 13:26:01 CET 2013


On 12/2/13 7:04 AM, Chris Angelico wrote:
> On Mon, Dec 2, 2013 at 10:58 PM, Ned Batchelder <ned at nedbatchelder.com> wrote:
>> Yes, a Queue object has a queue attribute:
>>
>>      >>> import Queue
>>      >>> q = Queue.Queue()
>>      >>> q.queue
>>      deque([])
>>
>> But you shouldn't use it.  It's part of the implementation of Queue, not
>> meant for you to use directly.  In particular, if you use it directly, you
>> are skipping all synchronization, which is the main reason to use a Queue in
>> the first place.
>
> I should apologize here; when the OP said "queue", I immediately
> noticed that I could import that and use it, and mistakenly started my
> testing on that, instead of using the deque type. It's deque that
> should be used here. Queue is just a wrapper around deque that adds
> functionality that has nothing to do with what's needed here.
>
> ChrisA
>

Actually, I had a long conversation in the #python IRC channel with the 
OP at the same time he was posting the question here, and it turns out 
he knows exactly how many entries are going into the "queue", so a 
plain-old list is the best solution.  I don't know quite where the idea 
of limiting the number of entries came from.

--Ned.




More information about the Python-list mailing list