[Discuss] A question for the Python gurus

Deryk Barker dbarker at camosun.bc.ca
Sat Jul 29 10:31:58 PDT 2006


Adam Parkin wrote:

> Deryk Barker wrote:
>
>> Ah, I blame C for this.
>
>
> LOL.
>
>> the structurally correct way is:
>>
>> line = sys.stdin.readline ()
>> while line:
>>    # do something with line
>>    line = sys.stdin.readline ()
>
>
> Ick ick ick, code duplication! =8-p  

A single line? Methinks you doth protest too much.

> Say you decide you don't want to read from stdin but rather stderr (or 
> maybe some other file object). Now you have two lines you have to 
> change and if the body of the while loop is large you could very 
> easily remember one and not the other.

No, because I have been coding loops this way for over 30 years.

>
> Or what happens when you're writing it at 3AM and you do something like:
>
> line = sys.stdin.readline()
> while line:
>     # do something
>     lines = sys.stdin.readline()
>
> Ooops, misspelled a variable and because Python doesn't force you to 
> declare a variable you now have an infinite loop (I've actually done 
> something like this in Perl before I discovered "use strict").

This is always a danger, writing loops this way doesn't make it any more 
or less dangerous.



More information about the Discuss mailing list