Detecting Infinite recursion in Python or dynamic languages
- by drozzy
Recently I tried compiling program something like this with GCC:
int f(int i){
if(i<0){ return 0;}
return f(i-1);
and it ran just fine. When I inspected the stack frames the compiler optimized the program to use only one frame, by just jumping back to the beginning of the function and only replacing the arguments to f. And - the compiler wasn't even running in optimized mode.
Now, when I try the same thing in Python - I hit maximum recursion wall (or stack overflow).
Is there way that a dynamic language like python can take advantage of these nice optimizations?
Maybe it's possible to use a compiler instead of an interpreter to make this work?
Just curious!