for-else

Troels Thomsen nejtak...
Wed Mar 5 22:44:03 CET 2008


>
> The primary use case is searching a container:
>
>   prep_tasks()
>   for item in container:
>       if predicate(item):
>           found_tasks()
>           break
>   else:
>       not_found_tasks()
>   follow_up_tasks
>

I've found myself mimicing this again and again in c, and was pleased to 
find it in python and use it regularely.
int i
for (i = 0 ; i < 10 ; ++i)
  blah
if i == 10
  not_found_tasks()

The discussion of words is silly. My surprise about "else following a for 
loop.... what the heck ...." lasted excactly as long as it takes to read 
this sentence.


tpt





More information about the Python-list mailing list