[Python-ideas] except expression

Ben Finney ben+python at benfinney.id.au
Thu Feb 13 01:14:12 CET 2014


Ram Rachum <ram.rachum at gmail.com> writes:

> 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"

That is more obscure, to my eye, than laying out the control branches:

    first_entry = entries[0] except IndexError else None
    item = my_queue.get() except queue.Empty else None
    try:
        response_text = request('http://whatever.com').text
    except HttpError:
        "Can't access data"

Do you have some real-use code that your proposal would significantly
improve? I find your example to support the refusal of that syntax.

-- 
 \            “The whole area of [treating source code as intellectual |
  `\    property] is almost assuring a customer that you are not going |
_o__)               to do any innovation in the future.” —Gary Barnett |
Ben Finney



More information about the Python-ideas mailing list