what should __iter__ return?
Terry Reedy
tjreedy at udel.edu
Wed Sep 8 16:17:02 EDT 2010
On 9/7/2010 9:40 PM, Gregory Ewing wrote:
> Thomas Jollans wrote:
>
>> Hmm. Modifying an object while iterating over it isn't a great idea,
>> ever:
>
> I wouldn't say never.
How about "Modifying a collection while iterating over it without
understanding the dangers is a bad idea."
" Algorithms that calculate some kind of
> transitive closure can be expressed rather neatly by appending
> items to a list being iterated over.
Which is one reason list modification while iterating is not prohibited.
Deleting items while forward iterating and adding items while backward
iterating may miss items unexpectedly. Adding items ahead of forward
iteration and deleting items behind backward iterating are ok. The most
common example of the latter is successively popping item off a stack.
--
Terry Jan Reedy
More information about the Python-list
mailing list