Quint van Dijk
Quint van Dijk

Reputation: 362

How to handle different return values from a function

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

Answers (1)

RemcoGerlich
RemcoGerlich

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

Related Questions