On Fri, Feb 07, 2014 at 05:41:32PM +1100, Chris Angelico wrote:
I honestly wouldn't worry. How often, in production code, will you iterate over something that might return an exception, AND be testing for the raising of that same exception? Or the converse - how often would you be in a position to raise the thing you might want to return, and be annoyed that the raised exception gets squashed into StopIteration?
Doesn't matter how rare it is. Doesn't matter if nobody has ever written production that does it. Or for that matter, if nobody has ever written *non-production* code that does it. "Special cases aren't special enough to break the rules." Exceptions are values just like strings and lists and floats. Probably nobody has ever written this particular piece of code: iter(func, "NOBODY expects the Spanish Inquisition!!!") but would you think it is okay to treat that particular sentinel value differently from every other sentinel value?
Don't complicate a nice simple API for the sake of that.
Your idea of simple is not the same as mine. The current behaviour "the callable is called until it returns the sentinel" is simpler than the proposed behaviour: "the callable is called until it returns the sentinel, unless the sentinel is an exception instance or class, in which case the callable is called until it raises that exception, or one which is compatible with it" This is an ugly API that violates the Zen of Python. I think that is why Raymond has listed this as a recipe rather than modify the function to behave as suggested: http://code.activestate.com/recipes/577155 -- Steven