Python stack overflow novice will learn []

python3.5.4


Recursive function when most disgusting Could stack overflow (Stack overflow).
PS: Also a lot of people in the process of learning Python, often because there is no one good tutorial or guide leading their own easy to give up, and I built a Python exchange dress: a long time and its military while a stream of thought (numbers under homonym) conversion can be found, there are new Python tutorial projects can take, I do not understand the problem more exchanges with people inside will solve Oh!

How to solve?

Set recursion depth human
use python to write recursive procedure if recursion is too deep, then most likely because more than the system default recursion depth limit and error. General recursive default length of about 1000.
RuntimeError: maximum recursion depth exceeded in comparison

Obviously at this point we can artificially modified

SYS Import
sys.setrecursionlimit (1000000) # recursion depth values in parentheses
tail recursion optimization
NOTE: This is just a way of thinking science
solution method further recursive call stack overflow by optimizing tail recursion, and in fact tail recursive loop effect is the same, therefore, as the loop is a special tail recursive function is also possible.

Tail recursion means that, when the function returns, calls itself itself, and, return statement can not contain expressions. In this way, the compiler or interpreter you can do to optimize tail recursion, the recursive call to itself, no matter how many times, only occupy a stack frame, stack overflow situation does not occur.

The above fact (n) function due to the return n * fact (n - 1) multiplication expression is introduced, so it is not the tail-recursive. To change the tail-recursive manner, it requires a bit more code is mainly the product of each step should passed to a recursive function:

def fact(n):
return fact_iter(n, 1)

fact_iter DEF (NUM, Product):
IF NUM == 1:
return Product
return fact_iter (NUM - 1, NUM * Product)
can be seen, return fact_iter (num - 1, num * product) returns only the recursive function itself, num - 1 and num * product will be evaluated before the function call does not affect the function call.

fact (5) corresponding to fact_iter (5, 1) of the following call:

===> fact_iter (. 5,. 1)
===> fact_iter (. 4,. 5)
===> fact_iter (. 3, 20 is)
===> fact_iter (2, 60)
===> fact_iter (. 1, 120)
===> 120
when tail-recursive calls, if optimized, the stack will not grow, and therefore, no matter how many times the call will not cause a stack overflow.

Unfortunately, most programming languages ​​do not optimized for tail recursion, Python interpreter did not do optimization, therefore, even if the above fact (n) function into a tail-recursive manner, can lead to stack overflow.

Standard Python interpreter did not do tail recursion is optimized for any recursive function can stack overflow problems

Summary of
Method One: artificially modify the default length of the recursive
method two: artificial modification python interpreter, it is optimized, will be very interesting
--- PS: Also a lot of people in the process of learning Python, often because there is no one good tutorial or guide resulting in their own easy to give up, and I built a Python exchange dress: a long time and their weapons while under a stream of thought (digital homonym) conversion can be found, there are new Python tutorial projects can take, do not understand the problem and more with people inside the exchange will solve Oh!
Text and images in this article from the network with their own ideas, only to learn, exchange, not for any commercial purposes, belongs to original author, if any questions, please contact us for treatment.

Guess you like

Origin www.cnblogs.com/chengxuyuanaa/p/12129823.html