Let's consider the following code :
def f(x):
return x
sol = [f]
sol.append(lambda x: sol[0](x) + x)
sol.append(lambda x: sol[1](x) + x)
print(sol[0](1), sol[1](1), sol[2](1))
# 1 2 3
Doing in such a way I got what I want. Now, let's do the exact same thing but with a for loop
:
def f(x):
return x
sol = [f]
for i in range(1, 3):
sol.append(lambda x: sol[i](x) + x)
print(sol[0](1), sol[1](1), sol[2](1))
# RecursionError: maximum recursion depth exceeded.
Can someone explain me what's happening ?
At first glance, I thought that the problem was similar to that one : lambda function calls itself repeatedly until it bottoms out at the recursion limit. But I do not see why that could be the case.