我的儿子正在教自己Python,但是让我难以理解以下内容。为什么此类的搜索功能返回None
?
我们显然正在点击return
,因为没有它,函数会无限期地递归,并且在打印的self.found
值之上的行上是正确的。
class Search:
def __init__(self):
print("made")
self.n = 0
self.found = []
def search(self, Object, List):
if self.n == 0:
self.found.clear()
print("1:self.found =", self.found, "\nself.n =", self.n)
if len(List) == self.n:
self.n = 0
print("2:self.found =", self.found, "\nself.n =", self.n)
return self.found
if List[self.n] == Object:
self.found.append(self.n)
print("3:self.found =", self.found, "\nself.n =", self.n)
self.n = self.n + 1
print("4:self.found =", self.found, "\nself.n =", self.n)
self.search(Object, List)
else:
self.n = self.n + 1
print("5:self.found =", self.found, "\nself.n =", self.n)
self.search(Object, List)