Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!feeder.news-service.com!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!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.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'desired.': 0.07; 'exception,': 0.07; 'terry': 0.07; 'exception.': 0.09; 'optionally': 0.09; 'exception': 0.12; 'iterator': 0.16; 'propagate': 0.16; 'reedy': 0.16; 'cc:addr:python-list': 0.16; 'this:': 0.16; 'wrote:': 0.16; 'cheers,': 0.18; 'cc:no real name:2**0': 0.20; 'stuff': 0.22; 'cc:2**0': 0.22; 'header:In- Reply-To:1': 0.22; 'loop,': 0.23; 'optional': 0.23; 'sep': 0.23; 'pm,': 0.24; 'stack': 0.24; 'statement': 0.25; 'raise': 0.28; 'sat,': 0.28; 'explicitly': 0.29; 'unknown': 0.29; 'message- id:@mail.gmail.com': 0.29; 'cc:addr:python.org': 0.30; 'letting': 0.30; 'received:209.85.161.46': 0.31; 'received:mail- fx0-f46.google.com': 0.31; 'break': 0.32; 'probably': 0.33; 'idea': 0.34; 'clearly': 0.34; 'another.': 0.34; 'explicit': 0.34; 'subject:next': 0.34; 'try:': 0.34; 'received:209.85.161': 0.35; 'another': 0.37; 'received:google.com': 0.38; 'received:209.85': 0.38; 'should': 0.38; 'subject:: ': 0.39; 'prepared': 0.39; 'empty': 0.39; 'case': 0.39; "it's": 0.40; 'happens': 0.40; 'manner': 0.64; 'flow': 0.68 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type:content-transfer-encoding; bh=Hy1th2FNtwnRZlu89+0Dn5eDuBrVgtOgNRtjB0SnyZM=; b=d4L9jdWZu9Xib6rvra/syeKNY+I/3xzJmweRBbc57r35cYyOH5nvqcrIaHAp/LbAkC OD1U3E1JyxJRu89OpgP+m7AZWMiTrc1Qx178fyQfqIA5pyZHJ/E+c4Sfzl/2FElnRNAf mRKCqWG04dfUa7FK9QLvoFnHJrfRIEYssu1D4= MIME-Version: 1.0 In-Reply-To: References: From: Ian Kelly Date: Sat, 10 Sep 2011 22:01:02 -0600 Subject: Re: Idioms combining 'next(items)' and 'for item in items:' To: Terry Reedy Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Cc: python-list@python.org 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: 31 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1315713693 news.xs4all.nl 2503 [2001:888:2000:d::a6]:33105 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:13109 On Sat, Sep 10, 2011 at 1:36 PM, Terry Reedy wrote: > The statement containing the explicit next(items) call can optionally be > wrapped to explicitly handle the case of an empty iterable in whatever > manner is desired. > > try: > =A0 =A0 > except StopIteration: > =A0 =A0raise ValueError("iterable cannot be empty") The only time that's optional is when you're writing an iterator and the try-except would end up looking like this: try: # do stuff with next(items) except StopIteration: raise StopIteration And even then, it's probably a good idea to clearly document that you're allowing a StopIteration from one iterator to propagate up as a StopIteration for another. Apart from that case, whenever you call next() you should always be prepared to catch a StopIteration. Letting a StopIteration propagate up the stack to parts unknown is bad juju because it's a flow control exception, not an error-signaling exception. If it happens to propagate up to another for loop, then it will break out of the for loop, and the exception will simply be swallowed. Cheers, Ian