带列表的Python递归返回None [英] Python recursion with list returns None
本文介绍了带列表的Python递归返回None的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
def foo(a):
a.append(1)
if len(a) > 10:
print a
return a
else:
foo(a)
为什么此递归函数返回None(请参阅下面的记录)?我不太明白自己在做什么错.
Why this recursive function returns None (see transcript below)? I can't quite understand what I am doing wrong.
In [263]: x = []
In [264]: y = foo(x)
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
In [265]: print y
None
推荐答案
您不会在else
子句中返回任何内容:
You don't return anything in the else
clause:
else:
return foo(a)
这篇关于带列表的Python递归返回None的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文