[Python-ideas] Python-ideas Digest, Vol 131, Issue 106

Chris Angelico rosuav at gmail.com
Tue Oct 31 03:02:34 EDT 2017


On Tue, Oct 31, 2017 at 5:18 PM, Ivan Pozdeev via Python-ideas
<python-ideas at python.org> wrote:
>> raise WhitespaceDamagedException from None
>
>
> Thunderbird jerked on me big time. It never did anything like this before!
> Switched off Digest mode, individual messages aren't so complicated.
>
> def single(i):
>     try:
>         v =i.next()
>     except StopIteration:
>         raise ValueError('No items')
>     try:
>         i.next()
>     except StopIteration:
>         return v
>     else:
>         raise ValueError('More than one item')
>
> print single(name for name in('bob','fred') if name=='bob')
>

Thanks :)

One small change: If you use next(i) instead of i.next(), your code
should work on both Py2 and Py3. But other than that, I think it's
exactly the same as most people would expect of this function.

ChrisA


More information about the Python-ideas mailing list