0

I would've thought a question like this should be answered but it seems I can't find any of the solution in google.

So anyawy. Can anyone give me or link me a builtin function where it will check whether the function is infinite recursing?

A function that looks like this will be awesome

def Check(InputFunction):
    if InputFunction is infinite recursing 
       print("blablla")/throw exception
    else
       run inputFunction

Is there something like that in python?

jrd1
  • 10,358
  • 4
  • 34
  • 51
Jimson
  • 25
  • 4

3 Answers3

3

Such a program does not exist. Not in Python, nor in any programming language.

What you're asking for is something called the "Halting Problem":

In computability theory, the halting problem is the problem of determining, from a description of an arbitrary computer program and an input, whether the program will finish running or continue to run forever.

REFERENCE:

http://en.wikipedia.org/wiki/Halting_problem

jrd1
  • 10,358
  • 4
  • 34
  • 51
  • It can be done by keeping a copy of the programs memory in each step and stop if you ever encounter the same memory at the same position in the code but it restricts the programs to be very small compared to the code that checks if it halts so the halting paradoxes where you have a program that uses such a function will be too large to examine. – Sylwester Aug 10 '14 at 21:35
2

This is equivalent to asking if we can solve the halting problem. This cannot be done. One way to check for a large number of recursive calls is to use a safety counter. This is a global numerical value that is incremented for each recursive call. If the counter reaches some extremely large value, you can throw an error and cause the recursion to stop.

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
  • 2
    It's worth noting that [Python already does this](http://stackoverflow.com/questions/3323001/maximum-recursion-depth). – user2357112 Aug 09 '14 at 07:08
  • @user2357112 Good to know. I'm a Python noob and just learned about this technique today while reading *Code Complete*, which still has some good suggestions even if it is a bit dated. – Code-Apprentice Aug 09 '14 at 07:12
0

The argument why this doesn’t work is this: you have your input_function is infinite recursing construct. Now I write this function:

def paradox():
    if paradox is infinite recursing:
        return True
    else:
        return paradox()

What do you expect the result of print paradox is infinite repeating to be?

Chronial
  • 66,706
  • 14
  • 93
  • 99