2.2 <-> 2.3 surprise

Shalabh Chaturvedi shalabh at cafepy.com
Mon May 31 13:28:57 EDT 2004


Roman Suzi wrote:

> Hi!
> 
> I really like python 2.3 but sometimes I write for 2.2 too.
> 
> New cool feature of doing:
> 
> f = open('file')
> for line in f:
>   do_something(line)
> 
> works strange in 2.2: I can't just quit first loop and do:
> 
> for line in f:
>   do_some_more(line)
> 
> (I as skipping message header by first loop and processing body
> the the second).
> 
> In 2.3 it works as intended! Of course, simple refacture made it one loop...
> 
> 
> 
> Sincerely yours, Roman Suzi

This is probably the following change as described in 
http://www.python.org/2.3/highlights.html

"File objects are now their own iterators. This makes multiple 
interrupted iterations over the same file more reliable."

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.

--
Shalabh






More information about the Python-list mailing list