T
T
Timon2932021-08-07 20:19:55
Python
Timon293, 2021-08-07 20:19:55

What is the best way to return?

Is there any difference in these 2 options? If yes, which one is better? Specifically, is it possible to do as in the second option for each if return or is it better to do one, as in the first example?

def test(a):
    if a == "abc":
        result = True
    elif a == "123":
        result = False

    return result


def test(a):
    if a == "abc":
        return True
    elif a == "123":
        return False

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alan Gibizov, 2021-08-07
@Timon293

In the specific example, nothing else is needed

def test(a):
    return a == "abc"

In general, I prefer one return, it's more direct in my opinion.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question