Simple if-else question
duncan.booth at invalid.invalid
Wed Sep 30 12:53:37 CEST 2009
Iain King <iainking at gmail.com> wrote:
> However, I assume you can get past the else by raising an exception,
> so the idea becomes a little muddled - do you warn when there is no
> break and no explicit raise caught outside the loop? What about an
> implicit exception? I would guess that code intentionally using an
> implicit exception to break out of a for loop is in need of a warning
> (and the author in need of the application of a lart), but I'm sure
> you could construct a plausible situation where it wouldn't be that
> Anyway, I'm ambivalently on the fence.
I think that in all cases of a for loop not containing break the following
are exactly equivalent:
If you exit from the loop by an exception or a return statement suite2 is
not executed in either case. The only case where the else clause makes a
difference is when you break from the loop.
So a warning for 'else' following a breakless for loop is not entirely
unreasonable as you can rewrite the code without the else.
However, if Python were going to start generating warnings though I'd much
rather see it warn or even error for accessing a guaranteed uninitialised
More information about the Python-list