[Python-ideas] PEP 479 and take()
Oscar Benjamin
oscar.j.benjamin at gmail.com
Sat Dec 13 13:27:45 CET 2014
On 10 December 2014 at 21:42, Greg Ewing <greg.ewing at canterbury.ac.nz> wrote:
>> On Wed, Dec 10, 2014 at 4:05 AM, Oscar Benjamin
>> <oscar.j.benjamin at gmail.com <mailto:oscar.j.benjamin at gmail.com>> wrote:
>
>>
>>
>> class TakeError(Exception):
>> pass
>>
>> def take(iterator, n=None):
>> if n is None:
>> try:
>> return next(iterator)
>> except StopIteration:
>> raise TakeError
>> else:
>> return tuple(take(iterator) for _ in range(n))
>
>
> Won't that just shift the problem from leaked StopIterations
> to leaked TakeErrors?
No because TakeError is an Error whereas StopIteration as a flow
control construct (as Ethan pointed out in another thread).
On one level it really doesn't matter what exception it is as long as
it's not StopIteration since StopIteration is caught in unexpected
places e.g. a for-loop. Of course introducing a completely new error
mitigates the possibility of masking some other error from the
underlying iterator.
Oscar
More information about the Python-ideas
mailing list