Question about exausted iterators

Diez B. Roggisch deets at nospam.web.de
Thu May 18 10:35:11 EDT 2006


Christophe wrote:

> Fredrik Lundh a écrit :
>> Christophe wrote:
>> 
>>> 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.
>> 
>> 
>> it's been coded like that because that's what the specification says:
>> 
>>     http://www.python.org/dev/peps/pep-0234/
> 
> I didn't though I had to mention that "Because the spec has been writen
> like that" wasn't a valid answer either.

The important thing is: it _is_ specified. And what about code like this:


iterable = produce_some_iterable()

for item in iterable:
    if some_condition(item)
       break
    do_something()

for item in iterable:
    do_something_with_the_rest()


If it weren't for StopIteration raised if the iterable was exhausted, you'd
have to clutter that code with something like

try:
   for item in iterable:
      do_something_with_the_rest()
except IteratorExhausted:
   pass

What makes you say that this is better than the above? Just because _you_
had some cornercases that others seems not to have (at least that
frequently, I personally can't remember I've ever bitten by it) isn't a
valid reason to _not_ do it as python does.

Besides that: it would be a major change of semantics of iterators that I
seriously doubt it would make it into anything before P3K. So - somewhat a
moot point to discuss here I'd say.

Diez



More information about the Python-list mailing list