[英]What is the most pythonic way to conditionally return a function
Say I have 2 functions. 说我有2个功能。 I want func2 to return func1 UNLESS func1 returns None, in which case func2 returns something else. 我希望func2返回func1 UNLESS func1返回None,在这种情况下func2返回其他内容。 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). 但这感觉不对,因为我必须两次调用func1(n)(并且func1(n)是一个更大的计算)。 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. 但这感觉不对,因为我认为我不应该分配一个永远不会再次使用的新变量,只是为了检查func1是否返回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? 有没有更简单的方法来执行此操作,我不必再调用func1两次,而且我不必创建新变量? 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) 我目前使用第二种方式(我将foo设置为func1返回的位置,而不是返回foo,除非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. 另外,请记住,在我的实际代码中,我调用了几个不同的函数,我想返回第一个不是None的函数,这只是一个更简单的代码版本,可以解决问题。
Since None
evaluates to False
, you could do: 由于None
评估为False
,您可以执行以下操作:
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.) 但是应该注意,如果func1(n)
返回任何falsey( 0
, []
等),这将导致func2
返回something_else
。
For many functions, you could use next
and some generator expressions : 对于许多函数,您可以使用next
和some generator表达式 :
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: 给调用func1的结果命名是相对便宜的,所以我会这样做,但是写这样的函数:
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. 你肯定不想两次调用func1
- 并且效率低下, func1
可能有副作用或根据当时的状态产生稍微不同的答案。
Also, there is no need for the else
after a return
as the return
exited the function. 此外, return
后不需要else
,因为return
退出函数。
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. 请注意,即使'func1'返回falsey值,这仍然有效。
Alternatively, noting the content of func1
, could you do: 或者,注意func1
的内容,你能做到:
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. 这取决于func1
的实际内容实际上是什么。
As a completely different take from my previous answer, based on your comment to iCodez: 根据您对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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.