Python recursive call overhead – RecursionError result before reaching the specified limit by setrecursionlimit
Question:
I am trying to make a basic power recursive function basicpower
as a part of my university assignment. I tried to set the recursion limit to 100 using sys.setrecursionlimit(100)
. When I make a call to basicpower
with any value between 98 and 100 if shows me a RecursionError: maximum recursion depth exceeded in comparison
error. However, if I make a call to basicpower
with any value between 1 and 97 it works perfectly fine. What causes this overhead in the call stack? Why I can not use the whole number specified by sys.setrecursionlimit
?
Code of basic power function:
import sys
sys.setrecursionlimit(100)
def basicpower(x, n):
'''Compute the value x**n for integer n.'''
if n == 0:
return 1 # base case without base case our recursive function will run forever
else:
return x * basicpower(x, n-1) # our recursive call so we are calling the function on itself on a smaller problem space
print(basicpower(2,98))
Answers:
So something to point out first, since you have your last check as n==0, your total stack for the recursive call will be n+1. an n of 98 is already 99 stacks.
After this, I’m not too sure about the details since it involves some underlying functionalities of python that I’ve never looked into. However, sys.setrecursionlimit
is simply determining the total stack limit of the python interpreter. You can improve your recursion as
def basicpower(x, n):
'''Compute the value x**n for integer n.'''
if n == 1:
return x # base case without base case our recursive function will run forever
else:
return x * basicpower(x, n-1) # our recursive call so we are calling the function on itself on a smaller problem space
This would reduce the amount of stacks required by the recursion call to complete by 1. If you are required to be able to get to 100 while the setrecursionlimit is also 100, you my be able to do this if its allowed by your course.
if n==2:
return x*x
Of course, if you are trying to do this practically, simply doing x**n
would be better.
You can actually check the existing stack for your functions.
import inspect
for item in inspect.stack():
print(item)
print(len(inspect.stack()))
Running this in a python script or the python console, you get a stack length of 1 and a frame of
# for python console
[FrameInfo(frame=<frame at 0x000001803C38EC00, file '<stdin>', line 1, code <module>>, filename='<stdin>', lineno=1, function='<module>', code_context=None, index=None)]
Running the code in a .ipynb file gives a stack length of 22, and running it in google colab gave me a stack length of 28. Not sure if these numbers will vary, but this demonstrates how the initial point where you start your recursion won’t be at 0, and can vary based on what type of python you are using.
I am trying to make a basic power recursive function basicpower
as a part of my university assignment. I tried to set the recursion limit to 100 using sys.setrecursionlimit(100)
. When I make a call to basicpower
with any value between 98 and 100 if shows me a RecursionError: maximum recursion depth exceeded in comparison
error. However, if I make a call to basicpower
with any value between 1 and 97 it works perfectly fine. What causes this overhead in the call stack? Why I can not use the whole number specified by sys.setrecursionlimit
?
Code of basic power function:
import sys
sys.setrecursionlimit(100)
def basicpower(x, n):
'''Compute the value x**n for integer n.'''
if n == 0:
return 1 # base case without base case our recursive function will run forever
else:
return x * basicpower(x, n-1) # our recursive call so we are calling the function on itself on a smaller problem space
print(basicpower(2,98))
So something to point out first, since you have your last check as n==0, your total stack for the recursive call will be n+1. an n of 98 is already 99 stacks.
After this, I’m not too sure about the details since it involves some underlying functionalities of python that I’ve never looked into. However, sys.setrecursionlimit
is simply determining the total stack limit of the python interpreter. You can improve your recursion as
def basicpower(x, n):
'''Compute the value x**n for integer n.'''
if n == 1:
return x # base case without base case our recursive function will run forever
else:
return x * basicpower(x, n-1) # our recursive call so we are calling the function on itself on a smaller problem space
This would reduce the amount of stacks required by the recursion call to complete by 1. If you are required to be able to get to 100 while the setrecursionlimit is also 100, you my be able to do this if its allowed by your course.
if n==2:
return x*x
Of course, if you are trying to do this practically, simply doing x**n
would be better.
You can actually check the existing stack for your functions.
import inspect
for item in inspect.stack():
print(item)
print(len(inspect.stack()))
Running this in a python script or the python console, you get a stack length of 1 and a frame of
# for python console
[FrameInfo(frame=<frame at 0x000001803C38EC00, file '<stdin>', line 1, code <module>>, filename='<stdin>', lineno=1, function='<module>', code_context=None, index=None)]
Running the code in a .ipynb file gives a stack length of 22, and running it in google colab gave me a stack length of 28. Not sure if these numbers will vary, but this demonstrates how the initial point where you start your recursion won’t be at 0, and can vary based on what type of python you are using.