[Python-ideas] except expression

Neil Girdhar mistersheik at gmail.com
Wed Feb 19 16:45:45 CET 2014


Totally agree with these sentiments.  Another advantage is that this code 
is self-documenting.  These "defaulting signatures" usually benefit from a 
comment to remind the reader what's going on.  With the except clause, it's 
obvious.

On Wednesday, February 12, 2014 7:14:45 PM UTC-5, Ram Rachum wrote:
>
> I'm happy you're for it. Maybe one day we'll see a Python 4 with no second 
> argument to dict.get, getattr and so many others...
>
>
> On Thu, Feb 13, 2014 at 2:08 AM, Raymond Hettinger <raymond.... at gmail.com<javascript:>
> > wrote:
>
>>
>> On Feb 12, 2014, at 9:02 PM, Ram Rachum <ram.r... at gmail.com <javascript:>> 
>> wrote:
>>
>> Here's an idea that would help shortening code. Allow a ternary 
>> expression based on except, like so:
>>
>>     first_entry = entries[0] except IndexError else None
>>     item = my_queue.get() except queue.Empty else None
>>     response_text = request('http://whatever.com').text except HttpError 
>> else "Can't access data"
>>
>> Aside from the fact that this would be a big grammar addition, a big 
>> problem here is the usage of the `else` keyword, that when used with except 
>> usually means "what would happen if there wasn't an exception" and here 
>> means the opposite. But I couldn't think of a nicer syntax.
>>  
>> I realize that this is a big change and that most people would be opposed 
>> to this... But I guess I just wanted to share my idea :)
>>
>>
>> I would like to see something like this come to fruition.
>> We need a clean way to express the idea of "take an
>> arbitrary, exception-raising function and give it a default
>> argument".
>>
>> Hopefully, this would end the gradual but never-ending requests
>> to bloat APIs with "default" arguments.   For example, if your idea
>> or some variant had been in place, the min() and max() functions
>> likely wouldn't have grown complex signatures in Python 3.4.
>>
>>
>> Raymond
>>
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20140219/7dd63faa/attachment-0001.html>


More information about the Python-ideas mailing list