[Discuss] A question for the Python gurus

Brian Quinlan brian at sweetapp.com
Sat Jul 29 00:04:05 PDT 2006


Adam Parkin wrote:
> 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").

You get an infinite loop, as you said. Then you exit the program and fix 
it. If you are prone to this type of typo, use PyChecker or some other 
type of linting tool.

> That actually raises another thing I've often wondered about Python, is 
> there any Python equivalent to the "use strict" statement in Perl which 
> forces you to declare a variable with "my" before using it?

No, no such thing exists in Python.

> I agree that it's probably nicer than the C-style assignment, but I 
> disagree that it's as nice as say something like (in Python-ish syntax):
> 
> while (not stdin.eof()):
>     line = stdin.readline()
> 
>     # do something
> 
> Why is there no eof() method in the file object class in Python?

That's in interesting question. Probably because no one has ever needed 
one. You certainly don't in this case :-)

Cheers,
Brian


More information about the Discuss mailing list