<p>Your examples of else with loops are missing the extra behaviour on break? This is where the real value is in this usage. </p>
<div class="gmail_quote">On Nov 11, 2011 9:11 AM, "Terry Reedy" <<a href="mailto:tjreedy@udel.edu">tjreedy@udel.edu</a>> wrote:<br type="attribution"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
On Thu, Nov 10, 2011 at 10:53 AM, Guido van Rossum<<a href="mailto:guido@python.org" target="_blank">guido@python.org</a>> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
On Thu, Nov 10, 2011 at 5:35 AM, Rob Cliffe<<a href="mailto:rob.cliffe@btinternet.com" target="_blank">rob.cliffe@btinternet.<u></u>com</a>> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
"except: raise" makes it explicit that an exception is to be propogated.<br>
Far from being pointless it makes the code much more readable. "Explicit<br>
is better than implicit."<br>
</blockquote>
<br>
+1. Nobody's going to understand try...else... on first reading.<br>
</blockquote></blockquote>
<br>
Perhaps because an else: in this context, without being subordinate to anything, would be irrelevant, equivalent to 'pass' or, more exactly, 'if True:'.<br>
<br>
Else is always subordinate to some conditional clause$. The 'if' clause for try statements is the first except clause, not the try header: 'except x:' means something like 'if raised(x):' (since the corresponding try setup statement). The else clause is actually an elif clause: 'else:' means 'elif nothing_raised():'.<br>
<br>
To illustrate, the C equivalent of<br>
<br>
try:<br>
a = b/c<br>
except ZeroDivisionError:<br>
print('bah')<br>
else:<br>
print('hooray')<br>
<br>
would be (minus the punctuation that I have forgotten) something like<br>
<br>
errno = 0 /* setup error detection mechanism, just as try: does */<br>
a = b/c<br>
if errno == EZERODIV /* or whatever it is */<br>
printf('bah')<br>
elif errno == 0 /* ie, no error since setup */<br>
printf('hooray')<br>
<br>
I suspect Python *could* have been defined to require 'except NOEXCEPTION:' instead of 'else:' by initializing an internal exception variable (equivalent to C's errno) with NOEXCEPTION. Howver, 'else:' is definitely nicer (just as normal 'else:' is equivalent to and nicer than 'elif True:').<br>
<br>
<br>
$ A for loop is equivalent to a while loop with condition z"iterator (initially iter(iterable)) is not empty". Break and continue have the same meaning as in while loops.<br>
<br>
Any while loop, such as<br>
<br>
while c:<br>
x<br>
else:<br>
y<br>
<br>
is equivalent to an if statement with goto. In 'Python-with-label-and-goto' the above would be the same as<br>
<br>
label start_loop<br>
if c:<br>
x<br>
goto start_loop<br>
else:<br>
y<br>
<br>
So: in Python (as elsewhere), 'else' clauses always follow one or more headers that specify conditional execution and only trigger when the previous condition or conditions are false. Their use in various statement types is consistent and logically correct.<br>
<br>
-- <br>
Terry Jan Reedy<br>
<br>
______________________________<u></u>_________________<br>
Python-ideas mailing list<br>
<a href="mailto:Python-ideas@python.org" target="_blank">Python-ideas@python.org</a><br>
<a href="http://mail.python.org/mailman/listinfo/python-ideas" target="_blank">http://mail.python.org/<u></u>mailman/listinfo/python-ideas</a><br>
</blockquote></div>