On 2014-02-12 21:02, Ram Rachum wrote:
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.
If you don't mind having a colon in the middle of an expression: first_entry = entries[0] except IndexError: None item = my_queue.get() except queue.Empty: None response_text = request('http://whatever.com').text except HttpError: "Can't access data" What would its precedence be? Maybe it would apply to the preceding expression or subexpression: total = (entries[0] except IndexError: 0) + (entries[-1] except IndexError: 0)
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 :)