[Python-Dev] Chaining try statements: eltry?

Guido van Rossum gvanrossum at gmail.com
Wed Jul 6 21:02:14 CEST 2005


On 7/6/05, Thomas Lotze <thomas at thomas-lotze.de> wrote:
> I want to ask what you think about introducing a keyword 'eltry' which
> would be the counterpart of 'elif' for try statements. This had been
> suggested before on python-list a couple of years ago by Jonathan
> Gardner, but nothing (that I could find) seems to have come of it.

I'm -1 on this. The use case doesn't occur often in my experience and
IMO putting each try statement in the else clause of the previous one
is a fine solution.

I also notice that your only example is very repetitive, and would be
better written as a loop, using Python's dynamic nature:

for decoder in foo_decode, bar_decode, foobar_decode:
    try:
        data = decoder(data)
        break
    except ValueError:
        print "data doesn't seem to be %s-encoded" % decoder.__name__

-- 
--Guido van Rossum (home page: http://www.python.org/~guido/)


More information about the Python-Dev mailing list