2

I am trying to check the output of few functions and if there is no error I move to next function. So I have added a while loop and few if statements to handle the errors:

success = True
    while success:
        err, msg = function1()
        if not err:
            err, msg = function2()
            if not err:
                err, msg = function3()
                if not err:
                    err, msg = function4()
                else:
                    print msg
                    success = False
            else:
                print "function2 fails"
                sucess = False
        else:
            print "function1 fails"
            success = False

Is it a better way to avoid if,else , how can I redesign the code for this purpose?

3
  • 2
    Not sure If I understand you correctly but it's called exception handling: docs.python.org/2/tutorial/errors.html. You can use try except around the functions and in the functions you can raise and exception if an error occurs. Commented Feb 25, 2015 at 17:18
  • What you want to do is replace where your returning err with a raise statement, such as raise RuntimeError(). Then wrap the function calls in a try: ... except RuntimeError: ... block. Commented Feb 25, 2015 at 17:25
  • Use exceptions, duh. Commented Feb 25, 2015 at 17:29

2 Answers 2

3

A relatively simple way would be to create a list of functions and iterate through them:

functions = [function1, function2, function3, function4]
success = True
while success:
    for f in functions:
        err, msg = f()
        # If there's an error, print the message, print that the
        # function failed (f.__name__ returns the name of the function
        # as a string), set success to False (to break out of the while
        # loop), and break out of the for loop.
        if err:
            print msg
            print "{} failed".format(f.__name__)
            success = False
            break

I'm sure you could be much more fancy and create a custom iterator, etc, etc, etc (which might be a better solution if your actual needs are more complicated). But this should work, too.

If you're concerned about printing to STDERR instead of STDOUT, you can also use the warn function in the warnings module.

Sign up to request clarification or add additional context in comments.

Comments

1

You can try the following:

while True:
    for f in (function1, function2, function3, function4):
        err, msg = f()
        if err:
            print("%s failed, msg is %s" % (f.func_name, msg))
            break
    else:
        break

It executes each function in sequence. If one of them fails then the msg and function name is printed, and we break the for statement. When we break the for, the else is not executed. So there is one more repetition of the above cycle.

If every function runs successfully then we don't break and the else of for is executed. This breaks from while True and the program continues normally.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.