[Python-ideas] except expression

Bruce Leban bruce at leapyear.org
Wed Feb 19 09:23:47 CET 2014


On Tue, Feb 18, 2014 at 11:06 PM, Chris Angelico <rosuav at gmail.com> wrote:

> However, I'm removing two parts of the proposal: ... chaining of
>
 excepts (use parens, "(expr except Exception1: default1) except
> Exception2: default2").
>

There are two problems with trying to leave this out.

First, you say "use parens" but no use of parenthesis can have the same
effect. The alternative you give:
    (expr except E1: default1) except E2: default2
is not the same as the chained form. This catches E2 in both the evaluation
of E1 *and* the evaluation of default1 while the chained form catches both
exceptions only in the evaluation of E1. And putting parens anywhere else
doesn't do it either.

Second, isn't an except expression like any other expression which can be
used anywhere an expression can be? So that means that writing something
that looks like a chained expression without parenthesis *will* be a valid
expression and will have to have some interpretation (depending on whether
except is left associative or right associative.

Given that, I think it is better to define it as chained now rather than
leaving people to think it's chained and never being able to add it in the
future (since that will change the semantics of existing code). This is
analogous to handling a < b < c: it's a bug magnet in C for inexperienced
programmers but can't be changed without breaking code. Starting from
scratch in Python, it could be chained.

--- Bruce
Learn how hackers think: http://j.mp/gruyere-security
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20140219/f84f0c87/attachment-0001.html>


More information about the Python-ideas mailing list