while true: !!!

Carsten Geckeler nospam at no.spam
Mon Dec 18 13:20:14 CET 2000


On Mon, 18 Dec 2000, Delaney, Timothy wrote:

> 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.

Well, I would prefere the following:

while not f.eof():
	line = f.readline()
	# do stuff

but unfortunatly the Python file objects don't have this method.

Cheers, Carsten
-- 
Carsten Geckeler:  carsten dot geckeler at gmx dot de
To get proper email-address replace `dot' and `at' by the corresponding symbols.





More information about the Python-list mailing list