for ... else ?

exhuma.twn exhuma at gmail.com
Tue Jun 12 05:34:49 EDT 2007


On Jun 12, 6:57 am, "Gabriel Genellina" <gagsl-... at yahoo.com.ar>
wrote:
> [...]
>
> for number in range(10,100):
>      is_prime = True
>      for divisor in range(2,number):
>          if number % divisor == 0:
>              is_prime = False
>              break
>      if is_prime:
>          print number,
>
> Next step: for loops have an optional "else" clause, that gets executed
> whenever the loop exits normally (in this case, when divisor goes up to
> number, and the break statement is never executed). So you don't need
> is_prime:
>
> for number in range(10,100):
>      for divisor in range(2,number):
>          if number % divisor == 0:
>              break
>      else:
>          print number,
>

Oh my. Would it not be an idea to rename this "else" into a "finally"?
As Gabriel points out, the else-block gets executed after the for loop
exits *normally*. In that case, is the "else" not semantically
misleading? I would surely misunderstand it if I saw it the first time.




More information about the Python-list mailing list