Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #12964

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

From Chris Torek <nospam@torek.net>
Newsgroups comp.lang.python
Subject Re: Best way to check that you are at the beginning (the end) of an iterable?
Date 2011-09-08 14:21 +0000
Organization None of the Above
Message-ID <j4aj1302fg0@news3.newsguy.com> (permalink)
References <d53bfc89-b402-49a6-bd4a-baa28d708f8e@glegroupsg2000goo.googlegroups.com> <mailman.854.1315441399.27778.python-list@python.org>

Show all headers | View raw


In article <mailman.854.1315441399.27778.python-list@python.org>
Cameron Simpson  <cs@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.

Thus, feof() in C is sort of (but not really) useless.  (The actual
use cases are to distinguish between "EOF" and "error" after a
failed read from a stream -- since C lacks exceptions, getc() just
returns EOF to indicate "failed to get a character due to end of
file or error" -- or in some more obscure cases, such as the
nonstandard getw(), to distinguish between a valid -1 value and
having encountered an EOF.  The companion ferror() function tells
you whether an earlier EOF value was due to an error.)
-- 
In-Real-Life: Chris Torek, Wind River Systems
Intel require I note that my opinions are not those of WRS or Intel
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W)  +1 801 277 2603
email: gmail (figure it out)      http://web.torek.net/torek/index.html

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Best way to check that you are at the beginning (the end) of an iterable? Laurent <laurent.payot@gmail.com> - 2011-09-07 14:35 -0700
  Re: Best way to check that you are at the beginning (the end) of an iterable? Cameron Simpson <cs@zip.com.au> - 2011-09-08 08:48 +1000
    Re: Best way to check that you are at the beginning (the end) of an iterable? Laurent <laurent.payot@gmail.com> - 2011-09-07 16:22 -0700
    Re: Best way to check that you are at the beginning (the end) of an iterable? Laurent <laurent.payot@gmail.com> - 2011-09-07 16:22 -0700
      Re: Best way to check that you are at the beginning (the end) of an iterable? Cameron Simpson <cs@zip.com.au> - 2011-09-08 10:23 +1000
        Re: Best way to check that you are at the beginning (the end) of an iterable? Laurent <laurent.payot@gmail.com> - 2011-09-07 17:53 -0700
        Re: Best way to check that you are at the beginning (the end) of an iterable? Laurent <laurent.payot@gmail.com> - 2011-09-07 17:53 -0700
        Re: Best way to check that you are at the beginning (the end) of an iterable? Chris Torek <nospam@torek.net> - 2011-09-08 14:21 +0000
          Re: Best way to check that you are at the beginning (the end) of an iterable? Cameron Simpson <cs@zip.com.au> - 2011-09-09 08:39 +1000
      Re: Best way to check that you are at the beginning (the end) of an iterable? Tim Chase <python.list@tim.thechases.com> - 2011-09-07 19:01 -0500
        Re: Best way to check that you are at the beginning (the end) of an iterable? Laurent <laurent.payot@gmail.com> - 2011-09-07 18:08 -0700
        Re: Best way to check that you are at the beginning (the end) of an iterable? Laurent <laurent.payot@gmail.com> - 2011-09-07 18:08 -0700
      Re: Best way to check that you are at the beginning (the end) of an iterable? Terry Reedy <tjreedy@udel.edu> - 2011-09-07 21:06 -0400
    Re: Best way to check that you are at the beginning (the end) of an iterable? Peter Otten <__peter__@web.de> - 2011-09-09 13:04 +0200
      Re: Best way to check that you are at the beginning (the end) of an iterable? Chris Angelico <rosuav@gmail.com> - 2011-09-09 21:30 +1000
  Re: Best way to check that you are at the beginning (the end) of an iterable? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-09-08 10:24 +1000
    Re: Best way to check that you are at the beginning (the end) of an iterable? Terry Reedy <tjreedy@udel.edu> - 2011-09-07 21:08 -0400
    Re: Best way to check that you are at the beginning (the end) of an iterable? Laurent <laurent.payot@gmail.com> - 2011-09-07 18:05 -0700
  Re: Best way to check that you are at the beginning (the end) of an iterable? Miki Tebeka <miki.tebeka@gmail.com> - 2011-09-07 17:24 -0700
    Re: Best way to check that you are at the beginning (the end) of an iterable? Laurent <laurent.payot@gmail.com> - 2011-09-07 18:06 -0700
    Re: Best way to check that you are at the beginning (the end) of an iterable? Chris Rebert <clp2@rebertia.com> - 2011-09-07 19:27 -0700

csiph-web