开发者

In Python, is there a clean way to return the value of a function if it's not None?

I find myself writing a lot of code that resembles the following:

ans = call_function()
if ans:
    return ans

...

Is there a clean way to m开发者_StackOverflow社区ake this a 1 or 2 liner? An "example" of such a paradigm might be

if x as call_function():
    return x


It seems that as long as ans is not None, you can just return it, based on your implied usage.

if ans is not None:
    return ans


I'm not sure what you're doing after your if, but perhaps you're doing the following:

ans = call_function()
if ans:
    return ans
else:
    return default

In which case it you can simply do:

return call_function() or default


It may make sense to refactor the code which would follow your if statement into another function. There is a (not unwise) school of thought which emphasizes making each function do one very specific thing.

In this case, you could write something like this:

ans = call_function()
return ans if ans is not None else following_code()

or, if you are really testing for a truthy value (rather than specifically testing for not None):

return call_function() or following_code()

In the not None case, you can still avoid assigning to the temp variable ans by writing a function like this:

def fallback(test_value, routine, *args, **kwargs):
    return test_value if test_value is not None else routine(*args, **kwargs)

and then using it like this:

return fallback(call_function(), following_code,
                arg_to_following_code, following_code_kwarg=keywordarg_value)

This might be useful if you're doing this sort of thing very frequently, but in general it will just make your code a bit harder to read, because people will not be familiar with your fallback function. The original form used in your question is bulky, but it has a readily recognizable shape that people will be used to seeing. It also does things in a very measured fashion, one logical action per line, as is the norm in Python.

On the other hand, it can be good to cut out extraneous local variables, since these can lead to bugs due to typos or confusion about scope.


Wanting to embed assignments in if statements is probably one of the more common feature requests we see for Python. The problem is that such embedded assignment proposals typically only work for the very limited cases where the value you want to store and the condition you want to check are identical (e.g. your example falls into that trap and would be useless if you instead needed to check a more specific condition like if ans is not None:).

If the extra line really offends you, you can collapse the if statement to a one-liner (if ans: return ans). A lot of people hate that style, though.

However, I question your basic premise that "I want to know if this function returns something meaningful, and if it is, then that is the result of this function as well, otherwise I will fall back and calculate my result some other way" isn't sufficient justification for using a properly scoped local variable.

Knowing whether or not another function has finished the job for you sounds pretty damn important to me.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜