[Python-Dev] Chaining try statements: eltry?

Phillip J. Eby pje at telecommunity.com
Thu Jul 7 21:03:35 CEST 2005


At 02:48 PM 7/7/2005 -0400, Tim Peters wrote:
>[Guido, on {for,while}/else]
>...
> > The question remains whether Python would be easier to learn without
> > them. And if so, the question would remain whether that's offset by
> > their utility for experienced developers. All hard to assess
> > impartially!
>
>That's what I'm here for.  I like loop "else" clauses, but have to
>admit that (a) I rarely use them; (b) most of the time I use them, my
>control flow is on the way to becoming so convoluted that I'm going to
>rewrite the whole function soon anyway;

Interesting; I usually intentionally write "else" clauses intending to 
*clarify* the code.  That is, I often use it in cases where it's not 
strictly necessary, e.g.:

    for thing in something:
        if blah:
            return thing
    else:
        return None

Because to me this clarifies that 'return None' is what happens if the loop 
"fails".  Compare to:

    for thing in something:
        if blah:
            return thing
    return None

This looks to me at first glance like code that always returns None after 
looping over 'something'.

Of course, these are trivial examples where reading the whole thing isn't a 
big deal; I'm more likely to use it when the loop or surrounding logic are 
a bit more complex, but I do also use it for fairly simple cases.

The interesting thing is that I think it *clarifies* the intent of the 
code, but I guess maybe that's only true for a small subset of Python 
programmers.

I suppose I wouldn't raise much of a fuss if the feature went away in 3.0, 
but I think I would miss it.


>I also suspect that if they weren't in the language already, a PEP to
>introduce them would fail, because
>
>     still_looking = True
>     some loop:
>         if found it:
>             still_looking = False
>             break
>     if still_looking:
>         # what would have been in the "else" clause
>
>is clear and easy to write without it.

*shudder* Okay, you just convinced me.  "Else" should stay, because the 
above is much less readable and writable!



More information about the Python-Dev mailing list