[Tutor] for/else question

Lloyd Kvam pythontutor at venix.com
Fri Sep 12 00:34:56 EDT 2003


If you simply set variables (change program state) depending upon whether
a loop completes or breaks, then the else doesn't buy you much.  One of the
things python supports, without forcing it on you, is the ability to reduce
the amount of state information used in a program.

 > for x in (a,b,c):
 >    if x == y:
 >       variable = something
 >       break
 > else:
 >    aw shucks, didn't find anything.
 >    variable = 0
# might continue
if variable:
	#do what we can knowing x == y
else:
	#do what we do when no x == y

rather write this:
 > for x in (a,b,c):
 >    if x == y:
	#do what we can knowing x == y
 >       break
 > else:
 >    aw shucks, didn't find anything.
      #do what we do when no x == y

variable disappears from the program logic.  This is a good thing.  It is one
less piece of state information to manage.  Functional programming tries to
manage state by eliminating it.  OOP tries to manage state by encapsulating it.
Python supports both approaches.


Gabriel Cooper wrote:

> 
> python has this weird "else" after a for loop, like so:
> 
> for x in y:
>    do stuff
> else:
>    do other stuff
> 
> At first glance I thought this was really cool. Instead of hanging onto 
> "found = 1" variables like in C, I could just throw on this handy else 
> and (I presumed) it would, if my for loop never resulted in a success, 
> automatically kick in. But no, that's not the case. So I thought that it 
> would hit the else if you broke out of the for loop (so, perhaps, like 
> catching an exception). But no, that's not the case either. It turns out 
> that it always uses the else unless you break out of your loop. So it's 
> not really an else, it's more like an "if I didn't die in my loop" 
> catch. What use is that?
> 
> It would seem that the use is so that if you're running through a for 
> loop and are trying to /find something/ rather than /do something/ then 
> if you didn't find what you were looking for (and thus didn't break) 
> then you can set variables appropriately, e.g.:
> 
> for x in (a,b,c):
>    if x == y:
>       variable = something
>       break
> else:
>    aw shucks, didn't find anything.
>    variable = 0
> 
> But that's lame for a couple of reasons: first, how often do you use a 
> for loop to /look for stuff/ instead of /do stuff/? Secondly, isn't 
> breaking out of a loop considered "bad programming"? Much like GOTO's? 
> Finally, in the example above you would have simply set 'variable' to 
> zero to begin with, so you wouldn't use the else anyway!
> 
> So... yeah... Do /you/ use "else" with for loops? If so, when?
> 
> =]
> 
> 
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
> 

-- 
Lloyd Kvam
Venix Corp.
1 Court Street, Suite 378
Lebanon, NH 03766-1358

voice:	603-443-6155
fax:	801-459-9582




More information about the Tutor mailing list