On Wed, Feb 20, 2019 at 2:43 AM Chris Angelico <ros...@gmail.com> wrote: > That's because a generator function conceptually has three ways to > provide data (yield, return, and raise), but mechanically, one of them > is implemented over the other ("return" is "raise StopIteration with a > value"). For other raised exceptions, this isn't a problem.
Other functions also conceptually have three ways of returning: ordinary return with a value, a documented special return like KeyError, and pass-through exceptions. If the pass-through exception is KeyError, it gets conflated with the documented exceptional return, but correct code should handle them differently. It doesn't matter whether the syntax for the documented special return is "return x" or "raise KeyError(x)". I've never been screwed by this as badly with other exceptions as I was by StopIteration, but it's a general problem with the design of exceptions. I don't think exception specifications would solve that problem since you probably couldn't describe the KeyError's origin in the spec either. But that doesn't mean it isn't a problem. _______________________________________________ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/