PEP: statements in control structures

Michael Robin me at
Sun Oct 21 01:23:38 CEST 2001

Skip Montanaro <skip at> wrote in message > 
> Python 2.2 with iterator support already supports
>     for line in file:
>         ...

It also supports the equally nice:

     for char in file: 
     for record in file:

although they all do the same thing :)
That's why I wish that getting interators required
a specfic call such as:

     for line in file.iterlines():

This is self-documenting, and matches the syle of "file.readlines()",
"dict.iterkeys/values()" etc. I know that
unadorned-object-as-own-iterator looks nice, but for cases where it's
not apparent (via the elements that can be returned by the "container"
type) what will be iterated over, it's nice to have it explicit. For
example, I'd consider the native or cannonical element of a file the
character, not the line, and expect it to be the default iterator, if
there was one. (I guess once you're indoctrinated into Python, you (a)
read the docs, and (b) come to expect the default to be what's most
Hard to tell when the "explicit vs. implicit" rule is beaten by the
"idiomatic" rule...


More information about the Python-list mailing list