[Python-ideas] Control Flow - Never Executed Loop Body

Andrew Barnert abarnert at yahoo.com
Wed Mar 23 11:53:57 EDT 2016


On Mar 23, 2016, at 03:08, Michel Desmoulin <desmoulinmichel at gmail.com> wrote:

>> But I think it is a coherent proposal, even if it's not one I like. :)
> 
> And what do you think about adding except clauses to if ? with ? while ?

Well, they don't have the problem that for has (for is already implicitly handling a specific exception in a specific way; none of them are), so they're coherent even without a solution to that problem.

(With has the additional problem that it may not be immediately obvious on first glance whether the exit gets calls before or after the except clause, but if so, people can read the docs the first time they come across it and remember it after that.)

But I also think they're even less necessary. They'd all be pure syntactic sugar for nesting the statement and a try/except, so we'd be making the language more complicated to learn and remember, and encouraging more code that isn't backward compatible. That's not a huge cost, but the benefit isn't very big either. For _really_ short cases, I think we want except expressions (PEP 463); for longish code, there's nothing wrong with an explicit try; the range for which hiding an implicit try inside if and while would really improve things is small enough that I don't think the benefit outweighs the cost.

But maybe some good examples of realistic 3-liners that are significantly improved by the change would convince me (and, more importantly, convince a majority of the others who are skeptical), so I'll keep an open mind.



More information about the Python-ideas mailing list