[Python-ideas] should there be a difference between generators and iterators?

Bruce Leban bruce at leapyear.org
Fri Sep 5 08:43:06 CEST 2008


I think it makes sense that itertools should pass on throw, etc. I'd be
interested in whether anything would break on this change.

I don't think I like the other suggestions. Having an exception in some
random part of a loop throw *into* the iterator of the loop, just seems
weird. For the examples, you give, couldn't break_ do the throw itself?

   for line in filex:
>        if test1(line): break
>    for line in filex:
>        ...
>

This example convinces me that  3 could introduce untold chaos into existing
code. Wouldn't something like this make more sense?

  for line in filex:
    if test1(line): break
  finally:
    filex.close()

(And yes, I know the time machine can do that with one extra word and
perhaps with handles this better.)

To get __enter__ and __exit__ behavior for an iterator, can't you just wrap
it in class that provides that capability and calls close? You might need
itertools to have some support that extended iterator class but that seems
simpler.

--- Bruce
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20080904/910d7793/attachment.html>


More information about the Python-ideas mailing list