2.2 <-> 2.3 surprise

Duncan Booth me at privacy.net
Tue Jun 1 06:07:42 EDT 2004


Shalabh Chaturvedi <shalabh at cafepy.com> wrote in 
news:mailman.462.1086024331.6949.python-list at python.org:

> Something that *might* work in both 2.2 and 2.3 using multiple loops is 
> (not tested):
> 
> f = iter(open('file'))
> 
> ...and then use f as before.

That is almost the correct idiom to use for code that must work in both 2.2 
and 2.3. It helps though if you save both the file and the iterator in 
variables otherwise you have no way to close the file when you are 
finished. So something like:

theFile = open('file')
f = iter(theFile)

for line in f:
   ... do something and maybe break ...
for line in f:
   ... and so on ...

theFile.close()




More information about the Python-list mailing list