Reputation: 362
I want to make a function in Python that can return different things. How do I handle this correctly? I have something like this:
def function():
#do something, when everything went allright do:
return (int, int, int, int)
# if something went wrong
return False
x, y, z, a = function()
How do I make this work when the function returns False
?
Upvotes: 1
Views: 85
Reputation: 31250
In general, try to avoid this as it makes your functions cumbersome to work with. Raise an exception in case something went wrong, maybe a custom exception that has a "error_message" attribute if you need that, or something.
But you can use a function like yours with
result = function()
if result:
x, y, z, a = result
else:
panic()
Upvotes: 3