+ 33
Did you know that a lambda function can do recursion?
# fact for factrorial fact = lambda x: x*fact(x-1) if x>1 else 1 # When I realized this, I was very surprised.
4 Respuestas
+ 10
I have not gained enough practical experience yet and I am amazed at such things. I do not understand when the lambda function becomes defined for the fact variable. After that, it can use this variable in the definition of itself (for recursion). I thought intuitively that there should be a difference between lambda functions and named functions. I thought that for this reason lambda functions are faster than named functions. But apparently I was wrong and it is not.
+ 4
Ты должен заменить func(x-1) на fact(x-1)
Это уже не актуально, но все равно
+ 1
Honestly I do not find it surprising. However, your post is more insightful than many others I read. If I can ask, why you were surprised?
0
you must replace func with fact to make it work