【问题标题】:Why is Python function returning a NoneType?为什么 Python 函数返回 NoneType?
【发布时间】:2012-12-01 13:14:01
【问题描述】:

我在python中写了两个递归函数。

第一个接受参数,修改它们,然后返回一个值。

def Similarity(string, sstring1, index):
    if condition1:
        return index
    if condition2:
        #do something
    Similarity(string, string1, index)

第二个接受参数并对全局变量执行操作。

def getData(i, value):
    global dataList
    if condition:
        return list(suffixList)
    #do something
    getData(i, value)

这些函数可以完美运行并执行我需要的精确计算,但始终返回 NoneType。

我一直无法弄清楚为什么。

【问题讨论】:

  • 没有return smth but None 或根本没有返回的函数返回无。

标签: python nonetype


【解决方案1】:

你没有返回任何东西总是使用return 语句从函数返回值。 Python 确实使用函数中的最后一条语句作为返回值。

def getData(i, value):
    global dataList
    if condition:
        return list(suffixList)
    #do something
    return getData(i, value)

没有显式 return 的函数退出,而是返回 None

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-08-18
    • 1970-01-01
    • 2022-07-12
    • 2013-11-01
    • 2017-02-09
    • 2021-12-23
    • 1970-01-01
    相关资源
    最近更新 更多