empty clause of for loops

Sven R. Kunze srkunze at mail.de
Wed Mar 16 14:14:42 EDT 2016



On 16.03.2016 17:20, Terry Reedy wrote:
> On 3/16/2016 11:17 AM, Sven R. Kunze wrote:
>> On 16.03.2016 16:02, Tim Chase wrote:
>
>>> Does it annoy me when I have to work in other languages that lack
>>> Python's {for/while}/else functionality?  You bet.
>>
>> I can imagine that. Could you describe the general use-case? From what I
>> know, "else" is executed when you don't "break" the loop. When is this
>> useful?
>
> When one wants to know if the iterable contained an exceptional item 
> or not

I don't think that's all since I wouldn't need the "else" clause here. 
The important part here is that there is code attached to the case of 
"item found".

That naturally leads to what when I want to attach code to the case of 
"no item found"?

> or when one wants to know if the iterator is exhausted or not.

That's not 100% true, isn't it? The break could happen during iterating 
over the last item of the iterable.

I vaguely remember this being a problem, why I always needed to dismiss 
the "else" idea in my code because of that very corner case.


Best,
Sven



More information about the Python-list mailing list