exception handling for a function returning several values

Antoon Pardon apardon at forel.vub.ac.be
Sat Feb 12 14:07:14 EST 2005


On 2005-02-12, beliavsky at aol.com <beliavsky at aol.com> wrote:
> If a function that normally returns N values raises an exception, what
> should it return?

Maybe it shouldn't return anything but instead of cathing the
exception it should let the caller handle it.

> 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.

what about the following.

def foo(x):

  # code setting y and z
  return x,z

try
  y,z = foo(x)
except ... :
  # Handle errors.

-- 
Antoon Pardon



More information about the Python-list mailing list