My son is teaching himself Python, but has stumped me with the following. Why does the Search function on this class return None
?
We are obviously hitting the return
as without it the function recurses indefinitely, and on the line above the value of self.found
printed is correct.
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)