Silly question re: 'for i in sys.stdin'?

Tom Eastman tom at cs.otago.ac.nz
Sun Apr 3 22:12:42 EDT 2005


I'm not new to Python, but I didn't realise that sys.stdin could be called
as an iterator, very cool!

However, when I use the following idiom:

   for line in sys.stdin:
       doSomethingWith(line)

and then type stuff into the program interactively, nothing actually happens
until I hit CTRL-D.  I expected that 'doSomethingWith(line)' would execute
after every line I input into the program, just like what used to happen
with:

   while True:
      line = sys.stdin.readline()
      if line == '': break
      doSomethingWith(line)

What is the difference?

Thanks for your help!

      Tom






More information about the Python-list mailing list