Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: 2.2 <-> 2.3 surprise

Reply
Thread Tools

Re: 2.2 <-> 2.3 surprise

 
 
Shalabh Chaturvedi
Guest
Posts: n/a
 
      05-31-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



 
Reply With Quote
 
 
 
 
Duncan Booth
Guest
Posts: n/a
 
      06-01-2004
Shalabh Chaturvedi <(E-Mail Removed)> wrote in
news:(E-Mail Removed):

> 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()

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
What a surprise - MCSE 2000 Webster MCSE 6 09-28-2003 10:39 AM
70-227 - surprise... Mah - Dee MCSE 6 09-03-2003 10:55 PM
JDBC and surprise with scrolling chris Java 1 08-19-2003 05:27 PM
MCSA Surprise JTT MCSE 3 08-15-2003 02:19 AM
Google Search surprise Rusty Lillico Computer Support 13 07-07-2003 04:26 AM



Advertisments