Re: [Python-Dev] Rationale behind lazy map/filter

2015年10月13日 09:38:56 -0700

On Wed, Oct 14, 2015 at 3:08 AM, Random832 <[email protected]> wrote:
> If you are writing code that tries
> to resume iterating after the iterator has been exhausted, I have to
> ask: why?
A well-behaved iterator is supposed to continue raising StopIteration
forever once it's been exhausted. I don't know how much code actually
depends on this, but it wouldn't be hard to make a wrapper that raises
a different exception instead:
class iter:
 _orig_iter = iter
 def __init__(self, thing):
 self.iter = self._orig_iter(thing)
 self.exhausted = False
 def __iter__(self): return self
 def __next__(self):
 if self.exhausted: raise RuntimeError("Already exhausted")
 try: return next(self.iter)
 except StopIteration:
 self.exhausted = True
 raise
Play with that, and see where RuntimeErrors start coming up. I suspect
they'll be rare, but they will happen.
ChrisA
_______________________________________________
Python-Dev mailing list
[email protected]
https://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
https://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com

Reply via email to