假设一个函数some_function(x)
返回一个整数,我目前做如下:使用函数的返回值,如果满足条件,而无需使用一个额外的变量
def another_function():
# stuff...
result = some_function(x)
if result != 0:
return result
# else: more stuff (unrelated to result)...
我想要的是找到最佳写作方式
if some_function(x) != 0:
return some_function(x)
当然没有叫some_function(x)
两次。
有没有更多的“聪明”的地道方式来写在Python 3中,没有使用result
?
为什么要聪明?你有什么很好,清楚并做好这项工作。我们往往对我们自己有时太聪明:-) –
聪明的惯用方式就是你已经拥有的。没有进一步... –
@JimFasarakisHilliard我同意:> – direprobs