Friday Finking: Contorted loops

MRAB python at mrabarnett.plus.com
Fri Sep 10 11:36:39 EDT 2021


On 2021-09-10 12:38, Alan Gauld via Python-list wrote:
> On 09/09/2021 22:36, dn via Python-list wrote:
> 
>> Even in fairly modest Python constructs, we quickly repeal the one-in,
>> one-out philosophy because try...except operates by providing another
>> exit-path.
> 
> Exceptions are exceptional by their nature (or should be!) As such
> they can arguably be excused from the SP strictures.
> 
> But python complicates this tenet still further by adding an else
> clause to its loops. And complicating this still more is that these
> else clauses have almost exactly opposite effects.
> 
> while...else...
> 
> executes the else if the body of the loop does NOT get executed.
> 
> for...else...
> 
> executes the else iff ALL iterations of the for loop DO complete.
> 
[snip]

In both cases, it executes the 'else' part if it didn't break out of the 
loop. That's it.

If all of the iterations completed, then there was no break, so the 
'else' part is executed.

If there were no iterations, then there was no break, so the 'else' part 
is executed.

It's the same for both of them.


More information about the Python-list mailing list