Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!feeder.news-service.com!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'anyway': 0.03; '(at': 0.03; 'plenty': 0.03; '(of': 0.05; 'happily': 0.07; 'received:verizon.net': 0.07; 'terry': 0.07; 'worse': 0.07; 'python': 0.08; 'eof': 0.09; 'first:': 0.09; 'libraries.': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:80.91.229.12': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'received:lo.gmane.org': 0.09; 'runtime': 0.09; 'tuple': 0.09; 'programmer': 0.10; 'protocol': 0.15; 'blah': 0.16; 'boolean': 0.16; 'bring,': 0.16; 'hypothetical': 0.16; 'input.': 0.16; 'iterable,': 0.16; 'iterator': 0.16; 'occasions': 0.16; 'piped': 0.16; 'reedy': 0.16; 'subject: \n ': 0.16; 'subject:iterable': 0.16; 'wrote:': 0.16; 'cheers,': 0.18; 'jan': 0.19; 'badly': 0.21; 'header:In-Reply-To:1': 0.22; 'somewhere': 0.23; 'runs': 0.23; '(or': 0.23; 'pascal': 0.23; 'personally,': 0.23; 'pm,': 0.24; 'code': 0.25; "i'm": 0.27; 'function': 0.27; 'fact': 0.27; 'code,': 0.28; 'raise': 0.28; 'facility': 0.29; 'asking': 0.29; 'example': 0.30; 'arguments.': 0.30; 'behaves': 0.30; 'rarely': 0.30; 'separately': 0.30; 'situations': 0.30; 'subject:?': 0.31; 'usual': 0.31; 'dependent': 0.32; 'like.': 0.32; 'error.': 0.32; 'implement': 0.32; 'there': 0.33; 'to:addr :python-list': 0.33; 'calling': 0.33; '...': 0.34; 'header:User- Agent:1': 0.34; 'checking': 0.34; 'things': 0.34; 'function.': 0.34; 'integer': 0.34; 'like:': 0.34; 'try:': 0.34; 'header:X -Complaints-To:1': 0.35; 'trouble': 0.35; 'totally': 0.35; 'certain': 0.35; 'presence': 0.36; 'issue': 0.36; 'cheap': 0.37; 'using': 0.37; 'but': 0.37; 'something': 0.37; 'think': 0.38; 'received:org': 0.38; 'subject:: ': 0.39; 'header:Mime-Version:1': 0.39; 'why': 0.39; 'to:addr:python.org': 0.39; 'case': 0.39; 'subject: (': 0.39; "it's": 0.40; 'where': 0.40; 'thinking': 0.40; 'cost': 0.65; 'grab': 0.66; 'cameron': 0.67; 'easy.': 0.67; 'utilities': 0.67; 'storage': 0.67; 'subject:are': 0.70; 'offer': 0.72; 'serious': 0.78; 'subject:you': 0.81; 'adopt': 0.84; 'idiom': 0.84; 'no?': 0.84; 'stall': 0.84; 'sudden': 0.84; 'subject:Best': 0.93 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Terry Reedy Subject: Re: Best way to check that you are at the beginning (the end) of an iterable? Date: Wed, 07 Sep 2011 21:06:56 -0400 References: <20110908002314.GA15766@cskk.homeip.net> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: pool-74-109-121-73.phlapa.fios.verizon.net User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:6.0) Gecko/20110812 Thunderbird/6.0 In-Reply-To: <20110908002314.GA15766@cskk.homeip.net> X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 74 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1315444083 news.xs4all.nl 2503 [2001:888:2000:d::a6]:47182 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:12932 On 9/7/2011 8:23 PM, Cameron Simpson wrote: > On 07Sep2011 16:22, Laurent wrote: > | I totally understand the performance issue that an hypothetical > | "istail" would bring, even if I think it would just be the programmer's > | responsibility not to use it when it's not certain that an end can > | be detected. > > The trouble with these things is that their presence leads to stallable > code, often in libraries. Let the programmer write code dependent on > istail() without thinking of the stall case (or even the gratuitous > execution case, as in a generator with side effects in calling .next()) > and have that buried in a utilities function. > > 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() > ... > > that is it often thought that having offered the eof() function is a > design error. (Of course in the example above the usual python idiom > would win out from existing habit, but there are plenty of other > situations where is would just be _easy_ to rely of istail() in whatever > form.) > > | But I don't see why *adding* something like "ishead" would be so bad > | (at worse by using a boolean somewhere as you mentioned). > > It is not awful, but as remarked: > - extra storage cost to _every_ iterable, for a rarely used facility > - extra runtime cost to maintain the state > - _retroactive_ burden on _every_ iterator implementation presently > existing; every iterator sudden needs to implement and offer this > extra facility to be generate purpose use > - it is easy to provide the facility on the rare occasions when it is > needed > > Personally, I think point 3 above is the killer and 1 and 2 are serious > counter arguments. The iterator protocol is intentionally as simple as sensibly possible. > | Anyway I was just asking if there is something better than enumerate. So > | the answer is no? The fact that I have to create a tuple with an > | incrementing integer for something as simple as checking that I'm at > | the head just sounds awfully unpythonic to me. > > You can just use a boolean if you like. I have plent of loops like: > > first = true > for i in iterable: > if first: > blah ... > ... > first = False > > Cheap and easy. Cheers, Or grab and process the first item separately from the rest. it = iter(iterable) try: first = next(it) except StopIteration: raise ValueError("Empty iterable not allowed") for i in it: -- Terry Jan Reedy