readline in while loop

Gerhard Häring gh at ghaering.de
Fri May 23 11:20:33 EDT 2003


Edvard Majakari wrote:
> On Fri, 23 May 2003, antonmuhin at sendmail.ru wrote: 
>>f = file("test.txt", 'r')
>>for line in f:
>>	print line
>>f.close()

> Looks neat. I try to avoid Perl vs. Python flame fest, but are some
> constructs in Python context-sensitive (like in Perl), or is this some
> handy use of iterators in class file?

The later one. Files are iterable in Python:

 >>> import sys
 >>> sys.stdin
<open file '<stdin>', mode 'r' at 0x0083BDA0>
 >>> "__iter__" in dir(sys.stdin)
1
 >>> sys.stdin.__iter__()
<xreadlines.xreadlines object at 0x00886CB8>

the xreadlines object is the iterator you use implicitely in your for-loop.

-- Gerhard





More information about the Python-list mailing list