Python Forum
Lambda function recursion error - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Lambda function recursion error (/thread-25839.html)



Lambda function recursion error - DeadlySocks - Apr-13-2020

So basically i am having trouble with the following Code. Whenever i run it, in the 3rd iteration the programm throws a RecursionError.
lst=[[]]

while len(lst)<10:
    lst.append([])
    lst[-1].append(lambda x: x+5)
    
    for i in range(1, int(len(lst)/2+0.5)):
        for a in lst[i]:
            for b in lst[-1-i]:
                value1=a(b(100))
                if value1>0 and value1%1==0:
                    lst[-1].append(lambda x: a(b(x)))
I copied this out of a project and i just need to understand, why it doesn't work or how i can fix it. In the project the lambda function adds, subtracts, divides and multiplies the x value, hence the if-statement.

The error occurs in the 3rd last line (value=a(b(100)) and if i delete that one and the following line it works.
The error is not caused by the while-loop being iterated too many times.

Thanks in advance for your responses.


RE: Lambda function recursion error - deanhystad - Apr-13-2020

My guess is this:
if value1>0 and value1%1==0
I don't see how this will ever be False. You are adding integers, so value1%1 == 0 is always True, and I don't see how x is ever going to go negative, so x+5 is never going to be negative.

This code may work with a different lambda, but I don't see it working with the lambda you provide.