0

I am wondring about the reason for the high setrecursionlimit overhead at Google Colab.

I am trying to make a basic power recursive function basicpower as a part of my university assignment shown in the screenshot below. I tried to set the recursion limit to 100 using sys.setrecursionlimit(100) locally on my laptop. 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. Furthermore, Why does Colab prevent me from using 39 frames not only the three mentioned above? What causes this overhead in the call stack?

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))
  • set `n+3` limit – sahasrara62 Feb 16 '23 at 10:47
  • @sahasrara62 That's not enough and it doesn't explain it. – Kelly Bundy Feb 16 '23 at 10:50
  • @KellyBundy it's just a comment, not an answer – sahasrara62 Feb 16 '23 at 10:56
  • @sahasrara62 And what's the point of that comment? – Kelly Bundy Feb 16 '23 at 10:57
  • 1
    you need to keep some stack size for system/function call , so that's why set it `n+3` – sahasrara62 Feb 16 '23 at 10:57
  • 1
    @sahasrara62 Still unclear what's your point with that. And apparently it should rather be `n+39`. – Kelly Bundy Feb 16 '23 at 11:04
  • I know that I can make the limit bigger to avoid the overhead. I am curious about the reason behind this overhead in Google Colab. – Hossam Hamza Feb 17 '23 at 05:45
  • Why are you messing with the recursion limit? Most languages don't give you this ability for good reason, it's an arbitrary limit that's platform- and implementation-specific. Any Python environment needs to call some functions, so that accounts for the gap between the limit and your code. – ggorlen Feb 21 '23 at 17:35
  • Does this answer your question? [Why Python raises RecursionError before it exceeds the real recursion limit?](https://stackoverflow.com/questions/55560258/why-python-raises-recursionerror-before-it-exceeds-the-real-recursion-limit) – ggorlen Feb 21 '23 at 17:40
  • Saddly, it does not. I know that there are some calls other than the one my recursive function makes. The question is why google colab needs much more calls? – Hossam Hamza Feb 21 '23 at 22:09

0 Answers0