2 ms·
> I would expect that for a loop, an else clause would happen when the loop does _not_ finish normally, or if they loop is empty Suppose it worked like that an
by tzs 8y ago
> I would expect that for a loop, an else clause would happen when the loop does _not_ finish normally, or if they loop is empty
Suppose it worked like that and you had this code:
for i in l:
A()
if B():
break
C()
else:
D()
Wouldn't that be equivalent to:
for i in l:
A()
if B():
D()
break
C()
What your suggested else is doing is providing a place to put code to execute after a break in the loop, but we already have a place for such code: immediately in front of the break in the loop.
The only place that comes to mind where this would be useful is situations where you there is more than one
if ...:
D()
break
in the loop. With your suggested kind of else, you could move all the D()'s to an else after the loop.
- deleted 8y ago[deleted]