[Python-ideas] except expression

Raymond Hettinger raymond.hettinger at gmail.com
Thu Feb 13 01:08:01 CET 2014


On Feb 12, 2014, at 9:02 PM, Ram Rachum <ram.rachum at gmail.com> 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/20140213/59e4d660/attachment.html>


More information about the Python-ideas mailing list