[Python-ideas] except expression

Steven D'Aprano steve at pearwood.info
Wed Feb 19 09:14:59 CET 2014


On Wed, Feb 19, 2014 at 06:06:05PM +1100, Chris Angelico wrote:


> Despite the numbers saying that 'as' is almost completely unnecessary
> to the proposal, I'm leaving it in for the moment, pending more data.
> However, I'm removing two parts of the proposal: bare except clauses
> (use "except BaseException:" if you really want that) and chaining of
> excepts (use parens, "(expr except Exception1: default1) except
> Exception2: default2").


I think it will be helpful to include a section with a few definitions, 
because the term "chaining" is ambiguous. It could mean either:

* Multiple except clauses in a single expression (that is, n-ary 
  operator, for n > ). Example:

    (expression except SpamError: spam,
                except EggsError: eggs)


where the 5 operands here are

    - expression
    - SpamError
    - EggsError
    - spam
    - eggs



Or chaining could mean wrapping one except-expression inside another, 
such as:

    (expression except SpamError: spam) except EggsError: eggs

which has two expressions each with three operands:

    - expression
    - SpamError
    - spam

and

    - (expression except SpamError: spam)
    - EggsError
    - eggs


I think it is important to decide which one is chaining, and which one 
is not, include it in the PEP as a definition, and stick with it. I 
*strongly* suggest that chaining means the second case. That's what 
chaining means when we talk about method chaining:

obj.spam().eggs()  # call eggs method of the result returned by spam

and exception chaining:

raise ThisError from some_exception

I don't think that we should prohibit passing one except-expression as 
argument to another.

The first case, the n-ary form, can be deferred for later. But I don't 
think that's chaining.

It is also important to note that this are not, in general, the same 
thing! 



-- 
Steven


More information about the Python-ideas mailing list