I just saw an alternative construction for python's if-else
statements like (0, 1)[ x > 5]
and wanted to try it with recursion but for some reason it's not working. Just forget for a moment that it's unpythonic.
Here is an original code which I'm trying to replace with alternative:
def f(n):
return 1 if n == 1 else n * f(n - 1)
Alternative, which give recursion problem:
def f(n):
return (n * f(n - 1), 1)[n == 1]
What is the problem with alternative code?
The problem is that Python will always try to compute n * f(n - 1)
before indexing the tuple with [n == 1]
.
The result is that the function keeps calling itself until the process runs out of memory on the stack.
The first code avoids this by doing the n==1
check before the recursive call, and then not making the call if the check succeeds
Source:
https://docs.python.org/2/reference/expressions.html has a section on 'Evaluation order' stating that it is performed left to right.
Your first function short circuits. n * f(n - 1)
is only computed if n != 1
.
In the second function, Python must evaluate both elements to create the list
, so n * f(n - 1)
is always computed, even when the elements are never accessed. This results in infinite recursion.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With