Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #53383
| From | Terry Reedy <tjreedy@udel.edu> |
|---|---|
| Subject | Re: print function and unwanted trailing space |
| Date | 2013-08-31 19:57 -0400 |
| References | (4 earlier) <5221f0af$0$2412$426a34cc@news.free.fr> <CAPTjJmpkJbEMdhHO9n5uERS4umD_hi1hx5RHDzkv6iM68VXoSw@mail.gmail.com> <CAHVvXxSZ_Pm939idyA5R5ayj3daCJ2Lfw679jGRfiYDWpMiakQ@mail.gmail.com> <CAPTjJmqraDbbt_SQGyT3pxjKEn79DHjeuLS+HY1-w4S06_j_jg@mail.gmail.com> <CAN1F8qWKZoheKdbZvTo7yVdOiHQUByhby8=pwAqgn54PVktT_w@mail.gmail.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.429.1377993482.19984.python-list@python.org> (permalink) |
On 8/31/2013 7:15 PM, Joshua Landau wrote:
> On 31 August 2013 23:08, Chris Angelico <rosuav@gmail.com> wrote:
>> On Sun, Sep 1, 2013 at 1:43 AM, Oscar Benjamin
>> <oscar.j.benjamin@gmail.com> wrote:
>>> On 31 August 2013 16:30, Chris Angelico <rosuav@gmail.com> 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
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
print function and unwanted trailing space candide <candide@free.invalid> - 2013-08-31 10:17 +0200
Re: print function and unwanted trailing space Andreas Perstinger <andipersti@gmail.com> - 2013-08-31 10:43 +0200
Re: print function and unwanted trailing space candide <candide@free.invalid> - 2013-08-31 11:25 +0200
Re: print function and unwanted trailing space Peter Otten <__peter__@web.de> - 2013-08-31 12:31 +0200
Re: print function and unwanted trailing space candide <candide@free.invalid> - 2013-08-31 15:33 +0200
Re: print function and unwanted trailing space Peter Otten <__peter__@web.de> - 2013-08-31 15:59 +0200
Re: print function and unwanted trailing space candide <candide@free.invalid> - 2013-08-31 17:58 +0200
Re: print function and unwanted trailing space Chris Angelico <rosuav@gmail.com> - 2013-09-01 01:30 +1000
Re: print function and unwanted trailing space Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2013-08-31 16:43 +0100
Re: print function and unwanted trailing space Chris Angelico <rosuav@gmail.com> - 2013-09-01 08:08 +1000
Re: print function and unwanted trailing space Joshua Landau <joshua@landau.ws> - 2013-09-01 00:15 +0100
Re: print function and unwanted trailing space Terry Reedy <tjreedy@udel.edu> - 2013-08-31 19:57 -0400
Re: print function and unwanted trailing space candide <candide@free.invalid> - 2013-08-31 17:51 +0200
Re: print function and unwanted trailing space Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-08-31 11:16 +0000
Re: print function and unwanted trailing space Peter Otten <__peter__@web.de> - 2013-08-31 14:27 +0200
Re: print function and unwanted trailing space Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2013-08-31 13:37 +0100
Re: print function and unwanted trailing space candide <candide@free.invalid> - 2013-08-31 14:59 +0200
Re: print function and unwanted trailing space Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-08-31 15:18 +0000
Re: print function and unwanted trailing space Ned Batchelder <ned@nedbatchelder.com> - 2013-08-31 07:24 -0400
Re: print function and unwanted trailing space candide <candide@free.invalid> - 2013-08-31 15:51 +0200
Re: print function and unwanted trailing space Wayne Werner <wayne@waynewerner.com> - 2013-09-11 06:36 -0500
Re: print function and unwanted trailing space Albert Hopkins <marduk@letterboxes.org> - 2013-09-12 15:25 -0400
csiph-web