print function and unwanted trailing space

Terry Reedy tjreedy at udel.edu
Sat Aug 31 19:57:44 EDT 2013


On 8/31/2013 7:15 PM, Joshua Landau wrote:
> On 31 August 2013 23:08, Chris Angelico <rosuav at gmail.com> wrote:
>> On Sun, Sep 1, 2013 at 1:43 AM, Oscar Benjamin
>> <oscar.j.benjamin at gmail.com> wrote:
>>> On 31 August 2013 16:30, Chris Angelico <rosuav at 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




More information about the Python-list mailing list