Exception handling in Python 3.x
Ethan Furman
ethan at stoneleaf.us
Mon Dec 13 16:23:09 EST 2010
Ethan Furman wrote:
> Arnaud Delobelle wrote:
>>
>> I missed the start of this discussion but there are two simpler ways:
>>
>> def func(iterable):
>> for x in iterable:
>> print(x)
>> return
>> raise ValueError("... empty iterable")
>
>
> For the immediate case this is a cool solution.
Drat -- I have to take that back -- the OP stated:
> The intention is:
>
> * detect an empty iterator by catching StopIteration;
> * if the iterator is empty, raise a ValueError;
> * otherwise process the iterator.
Presumably, the print(x) would be replaced with code that processed the
entire iterable (including x, of course), and not just its first element.
~Ethan~
More information about the Python-list
mailing list