[Python-ideas] except expression
Rob Cliffe
rob.cliffe at btinternet.com
Wed Feb 19 19:56:52 CET 2014
On 18/02/2014 20:55, Chris Angelico wrote:
> On Wed, Feb 19, 2014 at 3:06 AM, Chris Angelico <rosuav at gmail.com> wrote:
>> I'm currently working on finding a bunch of examples from the stdlib
>> that could be translated. Will post them once I get the script sorted
>> out. As it's currently 3AM, though, that means I need to fry up some
>> bacon or something before I continue :)
> Alright, results are in.
>
> Script:
> https://github.com/Rosuav/ExceptExpr/blob/master/find_except_expr.py
> Output:
> https://github.com/Rosuav/ExceptExpr/blob/master/candidates.txt
> Annotated examples:
> https://github.com/Rosuav/ExceptExpr/blob/master/examples.py
I've gone through a very small code base of my own to collect some
statistics.
Ignoring duplication where a section of code was copied from one program
to another,
there were approximately:
15 instances where the new syntax could naturally be used, roughly
half were variable assignment, the rest were "return" statements.
Of these I judged there were 9 where the new form was short enough
or at least readable enough to be a clear improvement.
The other 6 were long and complicated enough to be fairly
indigestible using either the old or the new syntax.
I also found, although I wasn't originally looking for them:
- 2 instances where there was a temptation to abuse the new syntax:
var = expr except Exception1: ShutDownProgram()
- 8 instances where I wanted to do an operation and ignore certain
exceptions, this sort of thing:
try:
dosomething()
except ValueError:
pass
and found I wanted the syntax "dosomething() except ValueError:
pass".
Under the current proposal this could be achieved by
"dosomething() except ValueError: None", but this does not read as
naturally.
- 13 instances where I wanted to do something that could _not_ be
written as an expression and ignore errors, broken down as follows:
5 where I wanted to assign an expression value to a variable
but only if evaluating the expression did not raise an error (if it did
raise an error I wanted to leave the
variable untouched)
1 where I wanted to return the value of an expression, but do
nothing (NOT return) if evaluating the expression raised an error.
7 of the form "del x[y] except IndexError: pass" (so to speak)
So is there a case for extending the syntax to allow
expr except <exception-list>: pass
where the expression is used stand-alone (i.e. its value is thrown
away). "pass" could be semantically equivalent to "None" (re the
interactive interpreter).
Or even for allowing "except <exception-list>: pass" after other
statements such as "del"?
Overall there were 6 instances of an exception list more complicated
than a single exception name.
There were none of "except Exception1 as ...". So now I am willing to
go along with dropping "as" from the PEP; it is something that could
always be added later.
Best wishes
Rob Cliffe
_
_
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20140219/e793b5b0/attachment.html>
More information about the Python-ideas
mailing list