altering an object as you iterate over it?

Paul McGuire ptmcg at austin.rr._bogus_.com
Fri May 19 16:39:58 EDT 2006


"John Salerno" <johnjsal at NOSPAMgmail.com> wrote in message
news:WNpbg.2142$No6.46279 at news.tufts.edu...
> Paul McGuire wrote:
>
> > Your coding style is a little dated - are you using an old version of
> > Python?  This style is the old-fashioned way:
>
> I'm sure it has more to do with the fact that I'm new to Python, but
> what is old-fashioned about open()? Does file() do anything different? I
> know they are synonymous, but I like open because it seems like it's
> more self-describing than 'file'.
>

I think it is just part of the objectification trend - "f =
open('xyzzy.dat')" is sort of a functional/verb concept, so it has to return
something, and its something non-objecty like a file handle - urk!  Instead,
using "f = file('xyzzy.dat')" is more of an object construction concept - "I
am creating a file object around 'xyzzy.dat' that I will interact with." In
practice, yes, they both do the same thing.  Note though, the asymmetry of
"f = open('blah')" and "f.close()" - there is no "close(f)".  I see now in
the help for "file" this statement:

    Note:  open() is an alias for file().

Sounds like some global namespace pollution that may be up for review come
the new Python millennium (Py3K, that is).

> > Now to your question about why '\n' lines persist into your new list.
The
> > answer is - you are STILL UPDATING THE LIST YOUR ARE ITERATING OVER!!!
>
> Doh! I see that now! :)
>

Sorry about the ALL CAPS... I think I got a little rant-ish in that last
post, didn't mean to shout. :)

Thanks for being a good sport,
-- Paul





More information about the Python-list mailing list