Exception handling in Python 3.x

Steven D'Aprano steve+comp.lang.python at pearwood.info
Fri Dec 3 18:28:22 EST 2010


On Fri, 03 Dec 2010 10:15:58 -0800, Paul Rubin wrote:

> Steven D'Aprano <steve+comp.lang.python at pearwood.info> writes:
>> def func(iterable):
>>     it = iter(iterable)
>>     failed = False
>>     try:
>>         x = next(it)
>>     except StopIteration:
>>         failed = True
>>     if failed:
>>         raise ValueError("can't process empty iterable")
>>     print(x)
> 
> Untested:
> 
>     from itertools import islice
> 
>     def func(iterable):
>        xs = list(islice(iter(iterable), 1))
>        if len(xs) == 0:
>           raise ValueError(...)
>        print xs[0]


If you're intention was to make me feel better about the version above 
that sets a flag, you succeeded admirably!

:)


-- 
Steven






More information about the Python-list mailing list