[Python-ideas] `__iter__` for queues?

Terry Reedy tjreedy at udel.edu
Wed Jan 20 03:06:16 CET 2010


On 1/19/2010 4:20 PM, cool-RR wrote:
>
>
> On Tue, Jan 19, 2010 at 11:10 PM, Simon Brunning
> <simon at brunningonline.net
> <mailto:simon at brunningonline.net>> wrote:
>
>     2010/1/19 cool-RR <cool-rr at cool-rr.com
>     <mailto:cool-rr at cool-rr.com>>:
>      > Is there a reason that queues don't have an `__iter__` method? I
>     mean both
>      > `Queue.Queue` and `multiprocessing.Queue`.
>
>     Could it be made threadsafe?
>
>     --
>     Cheers,
>     Simon B.
>
>
> For me, iterating on the queue means just calling `get` repeatedly until
> it's empty.

Explicit .get()s release the queue between getting each item so other 
code can also put and get. A mutable collection iterator may or may not 
lock the collection or monitor mutations. Dict iteration (and, I 
presume, set iteration, but I see nothing in the doc) monitors mutation. 
List iteration does not. While modifying a list during iteration can 
have uses, it is also leads to bugs and newby confusion. On the 
otherhand, one can write explicitly iterate dicts with

while d:
   k,v = dict.getitem()
   <go ahead and modify dict>

and the same with set.pop and have no problem.

So iterating with an iterator is not quite the same as repeated one-item 
fetches.

Terry Jan Reedy




More information about the Python-ideas mailing list