Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed1.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.004 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'else:': 0.03; 'string': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'try:': 0.09; 'val': 0.09; 'jan': 0.12; 'benjamin': 0.16; 'iterables': 0.16; 'iterator': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'reedy': 0.16; 'simpler,': 0.16; 'unsafe': 0.16; 'wrote:': 0.18; '>>>': 0.22; 'header:User- Agent:1': 0.23; 'error': 0.23; 'instead.': 0.24; 'pass': 0.26; 'header:X-Complaints-To:1': 0.27; 'header:In-Reply-To:1': 0.27; 'point': 0.28; 'chris': 0.29; 'am,': 0.29; 'raise': 0.29; "doesn't": 0.30; 'went': 0.31; '>>>>': 0.31; 'raised': 0.31; 'sep': 0.31; 'cases': 0.33; 'maybe': 0.34; 'except': 0.35; 'something': 0.35; 'but': 0.35; 'version:': 0.36; 'words,': 0.36; 'method': 0.36; 'similar': 0.36; 'should': 0.36; 'to:addr:python- list': 0.38; 'pm,': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'solve': 0.60; 'august': 0.61; 'received:173': 0.61; "you'll": 0.62; 'more': 0.64; 'oscar': 0.84; 'received:fios.verizon.net': 0.84; 'subject:space': 0.84; '2013': 0.98 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Terry Reedy Subject: Re: print function and unwanted trailing space Date: Sat, 31 Aug 2013 19:57:44 -0400 References: <5221a693$0$2059$426a74cc@news.free.fr> <5221b68b$0$2280$426a74cc@news.free.fr> <5221f0af$0$2412$426a34cc@news.free.fr> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: pool-173-75-251-66.phlapa.fios.verizon.net User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:17.0) Gecko/20130801 Thunderbird/17.0.8 In-Reply-To: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 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: 44 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1377993482 news.xs4all.nl 15974 [2001:888:2000:d::a6]:52994 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:53383 On 8/31/2013 7:15 PM, Joshua Landau wrote: > On 31 August 2013 23:08, Chris Angelico wrote: >> On Sun, Sep 1, 2013 at 1:43 AM, Oscar Benjamin >> wrote: >>> On 31 August 2013 16:30, Chris Angelico wrote: >>>>> >>>>> but doesn't solve all the cases (imagine a string or an iterator). >>>> >>>> Similar but maybe simpler, and copes with more arbitrary iterables: >>>> >>>> it=iter(range(5)) >>>> print(next(it), end='') >>>> for i in it: >>>> print('',i, end='') >>> >>> If you want to work with arbitrary iterables then you'll want >>> >>> it = iter(iterable) >>> try: >>> val = next(it) >>> except StopIteration: >>> pass # Or raise or something? >>> else: >>> print(val, end='') >>> for i in it: >>> print('', i, end='') >> >> I went with this version: >> >> except StopIteration: >> raise >> >> In other words, if it's going to bomb, let it bomb :) > > I think the point is that StopIteration is an unsafe error to raise. It should only be raised by iterator.__next__ method and caught by iterator user and not re-raised. Raise something like "ValueError('empty iterable') from None" instead. -- Terry Jan Reedy