Suppose the following:
def MyFunc(a):
if a < 0:
return None
return (a+1, a+2, a+3)
v1, v2, v3 = MyFunc()
# Bad ofcourse, if the result was None
What is the best way to define a function that returns a tuple and yet can be nicely called. Currently, I could do this:
r = MyFunc()
if r:
v1, v2, v3 = r
else:
# bad!!
pass
What I don’t like about this is that I have to use a single variable and then unpack it.
Another solution is I could have the function return a tuple full of Nones so that the caller can nicely unpack….
Anyone can suggest a better design?
How about raise an
ArgumentError? Then you couldtrycalling it, and deal with the exception if the argument is wrong.So, something like:
Also, see katrielalex’s answer for using a subclass of ArgumentError.