是否可以跳过“返回无”? [英] Is it ok to skip "return None"?

查看:127
本文介绍了是否可以跳过“返回无”?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不知道在不需要的时候跳过 return None 是不好的方法。

I wonder if it is bad manner to skip return None, when it is not needed.

示例:

Example:

def foo1(x):
    if [some condition]:
        return Baz(x)
    else:
        return None

def foo2(x):
    if [some condition]:
        return Baz(x)

bar1 = foo1(x)
bar2 = foo2(x)

如果条件为false,函数将返回 None

In both cases, when condition is false, function will return with None.

推荐答案

就像你说的那样, return None 是(几乎)不需要的。

Like you said, return None is (almost) never needed.

但是你应该考虑 您的代码的意图更加清晰, return None 。请记住:一段代码也需要被人类读取,明确的通常是有帮助的。

But you should consider that the intention of your code is much clearer with an explicit return None. Remember: a piece of code also needs to be readable by human-beings, and being explicit usually helps.

这篇关于是否可以跳过“返回无”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆