while true: !!!

Delaney, Timothy tdelaney at avaya.com
Sun Dec 17 19:03:31 EST 2000


This is precisely the reason this thread exists ...

The idiom is actually

while 1:

	do something

	if condition:
		break

	do something else

Basically, it's for dealing with situations where you can't tell at the
beginning or end of a loop if the loop is completed - only somewhere in the
middle. The canonical example is

while 1:

	try to read a line from a file

	if we didn't get a line:
		break

	do something with the line

There are many ways of dealing with this, but they all have compromises - in
most cases either duplication of code, or an assumption of memory
availability.

Tim Delaney
Avaya Australia
+61 2 9352 9079

> -----Original Message-----
> From: D-Man [mailto:dman at res137a-039.rh.rit.edu]
> Sent: Sunday, 17 December 2000 4:33 AM
> To: morpheus at here.not.there
> Cc: python-list at python.org
> Subject: Re: while true: !!!
> 
> 
> On Sat, Dec 16, 2000 at 04:45:07PM +0000, Steve Lamb wrote:
> > 
> > while 1:
> >   if cond:
> >     break
> >   do something
> > 
> 
> why not
> 
> while ( !cond ) :
> 	do something
> 
> It involves less typing and more importantly shows the next reader
> when the loop should end.  I strongly dislike the fake infinite loops
> like "while 1 :".
> 
> -D
> 
> 
> PS.  I haven't been following most of this thread, so forgive me if
> this has already come up.
> 
> 
> -- 
> http://www.python.org/mailman/listinfo/python-list
> 




More information about the Python-list mailing list