[Python-ideas] except expression

Ram Rachum ram.rachum at gmail.com
Wed Feb 12 22:02:57 CET 2014


Hi,

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



Ram.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20140212/f8315f4e/attachment.html>


More information about the Python-ideas mailing list