If/then style question

Arnaud Delobelle arnodel at gmail.com
Fri Dec 17 11:59:37 EST 2010


Tim Golden <mail at timgolden.me.uk> writes:

> On 17/12/2010 15:53, Steve Holden wrote:
>
> [... snip example of for-else ...]
>
>> This construct appears to be unpopular in actual use, and when it comes
>> up in classes and seminars there is always interesting debate as people
>> discuss potential uses and realise there are useful applications.
>
> I use this not infrequently, and I like it when it seems to be an
> elegant way to express the code path. But I still misremember from
> time to time and assume that the "else" clause fires when the for
> loop is empty.

I use it from time to time, even though, like you, I used to always be
unsure when the "else:" suite would be executed.  I now remember this
idiom as the "break else" construct: either the loop breaks, or the
"else:" suite is executed.

-- 
Arnaud



More information about the Python-list mailing list