for what are for/while else clauses

Michele Simionato michele.simionato at
Fri Nov 28 07:31:46 CET 2003

magnus at (Magnus Lyck?) wrote in message news:<258fd9b8.0311271519.153f573d at>...
> First of all, I'm not sure it's a good idea to use
> exceptions like a poor (rich?) man's GOTO like this. :)
> If you persist, I'm not so sure your code is clearer than:
>         try:
>             for ORlist in includes:
>                 try:
>                     for filter in ORlist:
>                         for field in curr_fields:
>                             for item in record[field]:
>                                 if match(item, filter):
>                                     raise Found
>                     raise NotFound
>                 except Found:
>                     continue
>         except NotFound:
>             continue
>         else:
>             result.append(record)
> With the else clause on the for loop, just after the if
> statement, you are bound to confuse people. I would guess
> that most people who casually looked at that code would
> think that you had made an indentation mistake. Some would
> probably "correct" it.

I, for one, could get confused. BTW, I don't remember a single case in
all my code where I have used so much indentation. I would write some
helper function and avoid nesting as much as possible.


More information about the Python-list mailing list