"also" to balance "else" ?

Sion Arrowsmith siona at chiark.greenend.org.uk
Tue Jun 14 07:38:13 EDT 2005


Fredrik Lundh <fredrik at pythonware.com> wrote:
>Ron Adam wrote:
>> True, but I think this is considerably less clear.  The current for-else
>> is IMHO is reversed to how the else is used in an if statement.
>nope.  else works in exactly the same way for all statements that
>support it: if the controlling expression is false, run the else suite
>and leave the statement.

For example, consider the behaviour of:

condition = False
if condition:
    print "true"
else:
    print "false"

and

condition = False
while condition:
    print "true"
    break
else:
    print "false"

>From this, it's clear that while/else gets its semantics from if/else.
Then:

i = 0
while i < 10:
    print i
    i += 1
else:
    print "Done!"

for i in range(10):
    print i
else:
    print "Done!"

So for/else behaves while/else, hence for/else really is the same way
round as if/else. It may not be "intuitive", but it's consistent, and
personally I'd rather have that.

-- 
\S -- siona at chiark.greenend.org.uk -- http://www.chaos.org.uk/~sion/
  ___  |  "Frankly I have no feelings towards penguins one way or the other"
  \X/  |    -- Arthur C. Clarke
   her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump



More information about the Python-list mailing list