Behavior of the for-else construct

Cameron Simpson cs at cskk.id.au
Fri Mar 4 20:15:28 EST 2022


On 03Mar2022 14:24, computermaster360 <computermaster360 at gmail.com> wrote:
>I want to make a little survey here.
>
>Do you find the for-else construct useful? Have you used it in
>practice? Do you even know how it works, or that there is such a thing
>in Python?

I used Python for years before understanding the for-else (and 
therefore, without seeing a use for it). Finally made the time to wrap 
my head around it quite recently and have used it a few times since.

It is a little niche, but when you want it, it is very nice indeed.  
Certainly avoids managing a special flag variable (which can be fiddly 
and therefore fragile) for "did my loop not run to completion"?

I sort of wish it had both "used break" and "did not use break" 
branches, a bit like try/except/else. But not very much.

Cheers,
Cameron Simpson <cs at cskk.id.au>


More information about the Python-list mailing list