[Python-ideas] chained try (was Re: "maybe import"?)

Andrew Barnert abarnert at yahoo.com
Sat Dec 28 21:05:28 CET 2013


On Dec 28, 2013, at 8:10, Chris Angelico <rosuav at gmail.com> wrote:

> There is one thing, though, that I'm seeing of all this. Exception
> throwing is asymmetrical: you can attempt a series of statements until
> one fails, but there's no convenient syntax to attempt a series of
> statements until one succeeds. I wonder, could the more general case
> be solved? Is there a way to, without stupid stuff like eval, wrap up
> a few statements so they can be executed in a loop:
> 
> def import_any(statement_list):
>    for try_me in statement_list:
>        try:
>            # uhh, this is the bit I'm not sure about...
>            try_me() # this would work if they're functions instead!
>            return
>        except ImportError:
>            pass
>    raise ImportError
> 
> This works for a set of functions, but not for a bunch of "from this
> import that" statements. Would it be worth mangling the top of your
> script until it can be done with importlib or __import__? Doesn't seem
> nearly as clean, somehow.

This is a clever idea. 

If you could work out a clean syntax for the "try until one works" syntax, it would solve a wider range of problems than just this import issue.

It strikes me that there's some similarity with if/elif/else. Maybe just:

    try:
        stuff
    except try:
        other stuff
    except try:
        different stuff
    except Exception as e:
        deal with it

But there are a lot of open questions. First, "except try" looks horrible, but I can't think of anything better. If they all fail, do you only get the last exception, or are they all chained together? Would there be a use for a sequence like this without except on the end (so if they all fail, it just raises)? Can you put an exception type or tuple between the "except try"? What about an as clause (so the block could use it)?


More information about the Python-ideas mailing list