exception handling for a function returning several values

beliavsky at aol.com beliavsky at aol.com
Fri Feb 11 20:01:56 EST 2005


If a function that normally returns N values raises an exception, what
should it return? N values of None seems reasonable to me, so I would
write code such as

def foo(x):
    try:
       # code setting y and z
       return y,z
    except:
       return None,None

y,z = foo(x)

If I try to use y or z inappropriately when they are None, the program
will stop. An alternative is to return an error flag in addition to y
and z from function foo and check the value of the error flag in the
calling program. This seems a bit awkward.



More information about the Python-list mailing list