Best way to check that you are at the beginning (the end) of an iterable?

Cameron Simpson cs at zip.com.au
Fri Sep 9 00:39:44 CEST 2011


On 08Sep2011 14:21, Chris Torek <nospam at torek.net> wrote:
| In article <mailman.854.1315441399.27778.python-list at python.org>
| Cameron Simpson  <cs at zip.com.au> wrote:
| >Facilities like feof() in C and eof in Pascal already lead to lots of
| >code that runs happily with flat files and behaves badly in interactive
| >or piped input. It is _so_ easy to adopt a style like:
| >
| >  while not eof(filehandle):
| >    line = filehandle.nextline()
| >    ...
| 
| Minor but important point here: eof() in Pascal is predictive (uses
| a "crystal ball" to peer into the future to see whether EOF is is
| about to occur -- which really means, reads ahead, causing that
| interactivity problem you mentioned), but feof() in C is "post-dictive".
| The feof(stream) function returns a false value if the stream has
| not yet encountered an EOF, but your very next attempt to read from
| it may (or may not) immediately encounter that EOF.

Thanks. I had forgotten this nuance. Cheers,
-- 
Cameron Simpson <cs at zip.com.au> DoD#743
http://www.cskk.ezoshosting.com/cs/

                          "Where am I?"
                        "In the Village."
                       "What do you want?"
                         "Information."
                    "Whose side are you on?"
"That would be telling. We want information. Information. Information!"
                       "You won't get it!"
                 "By hook or by crook, we will."
                         "Who are you?"
                       "The new number 2."
                       "Who is number 1?"
                       "You are number 6."
              "I am not a number, I am a free man!"
                           [Laughter]



More information about the Python-list mailing list