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

Ivan Pozdeev vano at mail.mipt.ru
Tue Oct 31 02:18:48 EDT 2017


On 31.10.2017 8:37, python-ideas-request at python.org wrote:
> On Tue, Oct 31, 2017 at 3:50 PM, Ivan Pozdeev via Python-ideas 
> <python-ideas at python.org> wrote:
>> On 30.10.2017 17:32, Guido van Rossum wrote:
>>> This is a key example of a case where code speaks. Can you write an
>>> implementation of how you would want single() to work in Python code?
>>>
>>> On Mon, Oct 30, 2017 at 2:49 AM, Ivan Pozdeev via Python-ideas
>>> <python-ideas at python.org  <mailto:python-ideas at python.org>> wrote:
>>>
>>>      The initial post on the above link summarizes the suggested
>>>      implementation pretty well.
>>>
>> |defsingle(i): try: ||v =i.next()
>> |||exceptStopIteration:||||raiseException('No values')|||try: ||i.next()
>> ||exceptStopIteration: ||returnv||else: ||raiseException('Too many values')|
>> ||printsingle(name forname in('bob','fred')ifname=='bob')||| |
>>
>> ||
> raise WhitespaceDamagedException from None
>
> ChrisA

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')

-- 

Regards,
Ivan

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20171031/82fc1920/attachment.html>


More information about the Python-ideas mailing list