zoukankan      html  css  js  c++  java
  • 尾递归装饰器

    import sys
    
    
    class TailRecurseException(BaseException):
        def __init__(self, args, kwargs):
            self.args = args
            self.kwargs = kwargs
    
    
    def tail_call_optimized(g):
        """
        This function decorates a function with tail call
        optimization. It does this by throwing an exception
        if it is it's own grandparent, and catching such
        exceptions to fake the tail call optimization.
    
        This function fails if the decorated
        function recurses in a non-tail context.
        """
    
        def func(*args, **kwargs):
            f = sys._getframe()
            # 为什么是grandparent, 函数默认的第一层递归是父调用,
            # 对于尾递归, 不希望产生新的函数调用(即:祖父调用),
            # 所以这里抛出异常, 拿到参数, 退出被修饰函数的递归调用栈!(后面有动图分析)
            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 as e:
                        args = e.args
                        kwargs = e.kwargs
    
        func.__doc__ = g.__doc__
        return func
    
  • 相关阅读:
    【poj1008】Maya Calendar
    【CodeVS4093】EZ的间谍网络
    2016.6.19 模拟考试
    【poj1129】Channel Allocation
    【poj2676】Sudoku
    Java 2D API
    Java 2D API
    IntelliJ IDEA
    IntelliJ IDEA 使用说明(For Eclipse user)
    IntelliJ IDEA
  • 原文地址:https://www.cnblogs.com/bigcatbc/p/10629296.html
Copyright © 2011-2022 走看看