[Python-ideas] except expression

Chris Angelico rosuav at gmail.com
Wed Feb 19 09:29:00 CET 2014


On Wed, Feb 19, 2014 at 7:14 PM, Steven D'Aprano <steve at pearwood.info> wrote:
> 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.

I've been using it to mean the first case. The second case is simply
what happens when you use an except-expression as an operand to
another except-expression, and I'd be inclined to call that "nesting",
as it's effectively this:

try:
    try:
        _ = expression
    except SpamError:
        _ = spam
except EggsError:
    _ = eggs

But if that form is called "chaining", then what's the other form
called? Whatever it is, I need a word for it, because that one is the
one that entails additional syntax.

> 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

But not comparison chaining, which is this:

1 < x < 2

I'm okay with changing the word, but I need something to change it
_to_. I can't just 'del chaining' - without some other reference, the
whole concept would be garbage collected :)

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

Of course not, any more than anyone prohibits the nesting of try blocks.

> 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!

Indeed, they're not.

ChrisA


More information about the Python-ideas mailing list