python - Does a StopIteration exception automatically propagate upward through my iterator? -
i'm implementing iterator in python wraps around iterator , post-processes iterator's output before passing on. here's trivial example takes iterator returns strings , prepends filtered myiter:
each one:
class myiter(object): """a filter iterator returns strings.""" def __init__(self, string_iterator): self.inner_iterator = string_iterator def __iter__(self): return self def next(self): return "filtered myiter: " + self.inner_iterator.next()
when inner iterator finishes, raise stopiteration exception. need special propagate exception wo whatever code using my iterator? or happen automatically, resulting in correct termination of iterator.
you not have special - stopiteration
exception propageted automatically.
in addition - may want read yield
keyword - simplifies creation of generators/iterators lot.
Comments
Post a Comment