This article mainly introduces the recursive use of Python in the end, the more detailed analysis of the tail recursion principle and related use skills, very practical value, the need for friends can refer to the
This example describes the recursive use of Python in the tail. Share to everyone for your reference. The specific analysis is as follows:
If all recursive forms of calls in a function appear at the end of a function, we call this recursive function to be tail-recursive. This recursive call is the tail recursion when the recursive call is the last statement executed in the entire function body and its return value is not part of the expression. The characteristic of a tail recursive function is that it is not necessary to do anything in the regression process, which is important because most modern compilers use this feature to automatically generate optimized code.
When the compiler detects that a function call is tail-recursive, it overwrites the current active record instead of creating a new one on the stack. The compiler can do this because the recursive call is the last statement to be executed during the current active period, so when the call returns there is nothing else to do in the stack frame, so there is no need to save the stack frame. By overwriting the current stack frame instead of adding one over it, the stack space used is greatly reduced, making the actual running more efficient. Therefore, whenever possible we need to write recursive functions in the form of tail recursion.
|1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 This is the||# This is shows off a python decorator (# which implements tail call optimization. It # does this by throwing a exception if it's # it ' s own grandparent, and catching such # exceptions to recall the Stac K. Import SYS class Tailrecurseexception:def __init__ (self, args, Kwargs): Self.args = args Self.kwargs = Kwargs def tail _call_optimized (g): "" "This function decorates the a function with the tail call optimization. It does this by throwing a exception if it is it's own grandparent, and catching such exceptions to fake the tail call op Timization. This function fails if the decorated function recurses in a non-tail context. "" "Def func (*args, **kwargs): F = sys._getframe () if F.f_back and f.f_back.f_back and f.f_back.f_back.f_code = = F.f_Code:raise tailrecurseexception (args, Kwargs) else:while 1:try:return g (*args, **kwargs) except Tailrecurseexception, E:args = E.args Kwargs = E.kwargs func.__doc__ = g.__doc__ return func @tail_call_optimized def factorial (n, acc=1): "Cal Culate a factorial "if n = = 0:return acc return factorial (n-1, N*ACC) print factorial (10000) # Prints a big, big number, # but doesn ' t hit the recursion limit. @tail_call_optimized def fib (i, current = 0, next = 1): if i = = 0:return current Else:return fib (i-1, Next, current + Next) Print fib (10000) # also prints a big number, # but doesn ' t hit the recursion limit.|
I hope this article will help you with your Python programming.