简体   繁体   中英

What is the most pythonic way to conditionally return a function

Say I have 2 functions. I want func2 to return func1 UNLESS func1 returns None, in which case func2 returns something else. There are two ways that I could do this, but they both feel slightly wrong.

I could say:

def func1(n):
    if (condition):
        return foo

def func2(n):
    if func1(n) is not None:
        return func1(n)
    else:
        return something_else

But this feels wrong because I have to call func1(n) twice (and func1(n) is a larger computation). To get around that, I could say:

def func1(n):
    if (condition):
        return foo

def func2(n):
    foo = func1(n)
    if foo is not None:
        return foo
    else:
        return something_else

but this feels wrong because I don't think I should have to assign a new variable that will never get used again, just to check if func1 returned None.

Is there an easier way to do this where I don't have to call func1 twice and I don't have to create a new variable? If this is the only way, which of the two would you recommend? I currently have it using the second way (Where I set foo to what func1 returned, than return foo unless foo == None)

Also, keep in mind that in my real code, I call several different functions, and I want to return the first one that is not None, this is just a simpler version of code that gets the question across.

Since None evaluates to False , you could do:

def func2(n):
    return func1(n) or something_else

It should be noted however that this will cause func2 to return something_else if func1(n) returns anything falsey ( 0 , [] , etc.)


For many functions, you could use next and some generator expressions :

def myfunc(n):
    vals = (f(n) for f in (func1, func2, func3...))
    return next((v for v in vals if v is not None), something_else)

Giving a name to the result of calling func1 is relatively cheap, so I'd do that, but write the function like this:

def func2(n):
    ret = func1(n)
    return ret if ret is not None else something_else

You definitely don't want to call func1 twice - as well as being inefficient, func1 may have side effects or produce a slightly different answer depending on the state at the time.

Also, there is no need for the else after a return as the return exited the function.

A revised version of your second option would be:

def func1(n):
    if condition:
        return foo

def func2(n):
    foo = func1(n)
    if foo is None:
        return something_else
    return foo

Note that this works even if 'func1' returns a falsey value.

Alternatively, noting the content of func1 , could you do:

def func1(n):
    return foo

def func2(n):
    foo = func1(n)
    if condition:
        return foo
    return something_else

It depends on what the real content of func1 actually is.

As a completely different take from my previous answer, based on your comment to iCodez:

def func1(n):
    return ham

def func2(n):
    return jam

def func3(n):
    return spam

def mainfunc(n):
    for f in (func1, func2, func3):
        foo = f(n)
        if foo is not None:
            return foo

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM