Question about exausted iterators

Christophe chris.cavalaria at free.fr
Thu May 18 09:14:07 EDT 2006


looping a écrit :
> Christophe wrote:
> 
>>Ok, call me stupid if you want but I know perfectly well the "solution"
>>to that problem ! Come on, I was showing example code of an horrible
>>gotcha on using iterators.
>>
> 
> 
> OK, your are stupid ;-)
> Why asking questions when you don't want to listen answers ?

Because I'm still waiting for a valid answer to my question. The answer 
"Because it has been coded like that" or is not a valid one.

>>Instead of saying that all works as intended could you be a little
>>helpful and tell me why it was intended in such an obviously broken way
>>instead ?
> 
> Why an exausted iterator must return an Exception (other than
> StopIteration of course) ?

Because it's exausted. Because it has been for me a frequent cause of 
bugs and because I have yet to see a valid use case for such behaviour.

> Well an exausted iterator could be seen like an empty string or an
> empty list (or tons of others things), so you expect the code
> for car in "":
>   print car
> to return an Exception because it's empty ???

Of course not.

> It's your job to check the iterator when it need to be.

It's my job to avoid coding bugs, it's the language job to avoid placing 
pitfalls everywhere I go.



I must confess I have a strong opinion on that point. Not long ago I 
started working on some fresh code where I decided to use a lot of 
iterators and set instead of list if possible. That behaviour has caused 
me to lose quite some time tracking bugs.



More information about the Python-list mailing list