Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/opengl/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么这个python函数返回None_Python_Return Value - Fatal编程技术网

为什么这个python函数返回None

为什么这个python函数返回None,python,return-value,Python,Return Value,以下是函数: def genWords(): global wordsList; global current_char; global char_seq_cords; global current_sequence current_sequence = "%s%s" % (current_sequence, getChar(current_char)) testable_prefixes = map( lambda x: ["%s%s" % (current_seq

以下是函数:

def genWords():
    global wordsList; global current_char; global char_seq_cords; global current_sequence

    current_sequence = "%s%s" % (current_sequence, getChar(current_char))
    testable_prefixes =  map( lambda x: ["%s%s" % (current_sequence, getChar(x)), x], possible_chars() )
    valid_seqs = filter(lambda x: linguisticly_possible_seq(x[0]),
                        testable_prefixes)  # 2 length list with char seq and char cords second

    for i in valid_seqs:
        if (u"%s" % current_sequence).lower() in english_tree:
            wordsList.append(current_sequence)
        current_char = i[1]
        char_seq_cords.append(current_char)

        genWords()

    if not valid_seqs:
        wordsList.append(current_sequence)
        return wordsList
print genWords()
print wordsList
我希望
print genWords()
的输出是一个列表,而不是
None
。我之所以如此困惑,是因为最后一行
print words list
打印了预期的输出;此外,如果我在
返回单词列表
之前插入这一行,
打印单词列表
,我将再次获得预期的输出。我似乎不明白为什么函数返回None

如果您需要参考,请参阅此处的src:
只有满足
有效的条件,才能到达return语句(从而返回单词列表)

否则,函数“简单返回”,默认值为
None


请注意,在本例中,返回的不是空列表(这肯定不等同于
None
)。相反,如果不指定另一个返回值,
None
是每个函数返回的值。

如果无效
如果
有效
是空列表,则将返回
False
gen_words
返回
None
,因为如果
valid_seqs
为空,您没有定义任何返回值。

因为
valid_seqs
为空?您只在“not valid_seqs”为真时返回值。确保所有路径都返回一些内容。因为python中的空值为false,所以如果无效,此代码可能不会转到此语句
。\u seqs:wordsList.append(当前\u序列)return wordsList
,并且函数不会返回任何结果为“无”的内容