Detecting Infinite recursion in Python or dynamic languages

Posted by drozzy on Stack Overflow See other posts from Stack Overflow or by drozzy
Published on 2010-03-24T12:01:23Z Indexed on 2010/03/24 12:03 UTC
Read the original article Hit count: 356

Filed under:
|
|

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!

© Stack Overflow or respective owner

Related posts about python

Related posts about gcc