while true: !!!

D-Man dsh8290 at rit.edu
Mon Dec 18 05:57:56 CET 2000

On Mon, Dec 18, 2000 at 11:03:31AM +1100, Delaney, Timothy wrote:
> while 1:
> 	try to read a line from a file
> 	if we didn't get a line:
> 		break
> 	do something with the line

This is precisely where having assigment as an expression (like C/C++)
is useful :

while ( (line = file.readline()) != None ) :
	do something with the line

Even a do-while loop doesn't solve this idiom ideally, you need to
read the line before the loop test and the test must occur before
using the line.

Maybe adding some sort of peek function to file objects could solve this :

while ( file.exists_next_line() ) :
	line = file.readline()
	do something with the line


More information about the Python-list mailing list