没有 return 声明但没有 returning None?
No return statement but not returning None?
不是很理解 return
语句。
def func():
print("test")
print(func())
正如预期的那样,这给出了:
test
None
因为执行了func()
,打印了“test”。此外,由于没有 return
语句,None
值是 returned.
但是,在这段代码中,当我删除外部 print
语句时:
def func():
print("test")
func()
仅给出:
test
这里是我不明白的地方。我读到默认情况下,所有函数 return None
值。如果没有return
语句,那么你会得到None
。但是,在这两段代码中,我都没有 return
语句。为什么第二个代码也没有给出 None
值?
来自评论:
Oh, so when they say functions will return None, I have to print None out to see it? Returning None doesn't mean None being shown on the output terminal?
正确。
不是很理解 return
语句。
def func():
print("test")
print(func())
正如预期的那样,这给出了:
test
None
因为执行了func()
,打印了“test”。此外,由于没有 return
语句,None
值是 returned.
但是,在这段代码中,当我删除外部 print
语句时:
def func():
print("test")
func()
仅给出:
test
这里是我不明白的地方。我读到默认情况下,所有函数 return None
值。如果没有return
语句,那么你会得到None
。但是,在这两段代码中,我都没有 return
语句。为什么第二个代码也没有给出 None
值?
来自评论:
Oh, so when they say functions will return None, I have to print None out to see it? Returning None doesn't mean None being shown on the output terminal?
正确。