New to Python. For in loops curiosity

Roy Smith roy at panix.com
Wed May 14 07:38:26 EDT 2014


In article <2f08e970-1334-4e7f-ba84-14869708a73b at googlegroups.com>,
 Leonardo Petry <leonardo.petry.br at gmail.com> wrote:

> Basically my question is: Why is python not treating the contents of 
> [a file] as one long string and looping each character?

Because whoever designed the original file object decided that the right 
way to iterate over a file is line by line.  In Python (although, at 
this level of explanation, I could be describing pretty much any 
language which has iterators), there is an iterator protocol which 
implements two ideas:

1) There's a method to call to get the next item.

2) There's a way for that method to signal that you've reached the end.

Exactly what "the next item" means is up to whoever implements the 
iterator.  In this case, it was decided that the most convenient thing 
would be for "item" to mean "line".  If you really want to iterate over 
a file character-by-character, it's easy enough to write an adapter.  
Something like this (untested):

def getchar(f):
   for line in f:
      for c in line:
         yield c

Of course, if the native file iterator was character-by-character, then 
if you wanted it line-by-line, you would have to write the inverse, a 
function which accumulates characters until it sees a newline, and then 
returns that.  Neither one is fundamentally better, or more correct than 
the other.  One may just be more convenient for a particular use case.



More information about the Python-list mailing list