control structures (was "Re: Sins")
William Tanksley
wtanksle at hawking.armored.net
Sun Jan 9 22:09:58 EST 2000
On Sun, 09 Jan 2000 11:35:58 -0500, Colin J. Williams wrote:
>skaller wrote:
>> Gordon McMillan wrote:
>> > That's the reason that Python has the "for
>> > .. else" construct that William Tanksley
>> > hates so much.
>> > for i in X:
>> > if X[i]==e:
>> > # do the "found" thing
>> > break
>> > else:
>> > print "Not Found"
>> That doesn't work. The 'do the "found" thing'
>> is in the wrong place: it must be outside the loop,
>> since it is usually the 'normal continuation'
>> of the routine.
>Aside from that. It seems to me that the weakness of the
> for ...
> else
>structure is that the else clause is executed whether the
>'for' succeeds or not. The Language Reference Manual has:
> "The expression list is evaluated once; it should yield a
>sequence. The suite is then executed once for each item in the
>sequence, in the order of ascending indices. Each item in turn
>is assigned to the target list using the standard rules for
>assignments, and then the suite is executed. When the items are
>exhausted (which is immediately when the sequence is empty), the
>suite in the else clause, if present, is executed, and the loop
>terminates."
In case you were wondering, the reason Gordon mentions me above is that I
said the same thing you're about to. I still agree with you; but neither
one of us is going to live to see the change we've proposed.
A better choice would be to advocate some other way of doing what's needed
-- labelled breaks would work superbly. Even gotos would be better
(although I hope they're not the method chosen :-).
By the way, I think your reading of the current rules is wrong. The else
clause will not be executed in case of a break -- it doesn't always get
executed, as you imply.
The 'try' statement has an 'else' clause with a similar mistake in
definition. Argh.
>More useful would be to execute the else clause only if the sequence is
>empty. This might be expressed as:
> for x in L:
> ...
> otherwise ...
I agree.
>Colin W.
--
-William "Billy" Tanksley, in hoc signo hack
More information about the Python-list
mailing list