为什么这个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
,并且函数不会返回任何结果为“无”的内容