python 尾递归
来源:互联网 发布:知满天面试培训怎么样? 编辑:程序博客网 时间:2024/04/30 23:47
网上有篇搞笑文 python 程序员进化 http://coolshell.cn/articles/2082.html ,对那个尾递归产生了兴趣。尾递归是指以迭代计算过程执行递归定义的语句,在python等语言中一般都是需要while, do, for这样的特殊语法。而 http://code.activestate.com/recipes/474088/ 有人实现了一个不需要这些语句的python尾递归,巨强无比。
代码我拷过来,便于看一下
## {{{ http://code.activestate.com/recipes/474088/ (r1)#!/usr/bin/env python2.4# This program shows off a python decorator(# which implements tail call optimization. It# does this by throwing an exception if it is # it's own grandparent, and catching such # exceptions to recall the stack.import sysclass TailRecurseException: def __init__(self, args, kwargs): self.args = args self.kwargs = kwargsdef 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() 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_optimizeddef factorial(n, acc=1): "calculate 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_optimizeddef 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.## end of http://code.activestate.com/recipes/474088/ }}}
这个尾递归的实现运用了decorator和exception机制, 基本原理是判断是否递归,是递归则抛出异常,然后捕捉异常重新调用。
ps. sys._getframe() 返回当前frame 对象
举例说明一下, factorial(2),由于decortaor,调用顺序是:
--func ------(2, )----factorial(2, 1)------func----(1, 2) --------raise TailRecurException back to func ---(1,2) back to factorial(1,2) back ot func ----(2,) call factorial(1,2) . . .
基本情况就是这样, 这里也非常取巧,利用参数解析来达到递归的效果
如果像下面定义一个递归函数,就不能使用这个尾递归了
@tail_call_optimizeddef fff(n): if n == 0: return 1 return n*fff(n-1)print fff(5)
- python 函数递归 尾递归
- Python 尾递归优化
- python 尾递归
- python尾递归
- Python尾递归
- python 实现尾递归
- Python尾递归
- python的递归函数--含尾递归
- 【Python学习日记】递归和尾递归 以及Python
- Python 与尾递归优化
- Python递归
- Python递归
- python递归
- Python | 递归
- Python 递归
- python 递归
- 递归与伪递归区别,Python 实现递归与尾递归
- python 斐波拉契递归 尾递归 备忘录 动态规划 迭代
- Image解读
- csdn博客,从现在开始
- Linux如何进入启动命令行
- android ics打开/隐藏导航栏
- 编程之美2.14 子数组之和的最大值 扩展问题(首尾相连)
- python 尾递归
- 【计划】Spring Hibernate Struts (标题党)
- Linux查看mac地址
- Android中不能解析GIS类的XML文件或不能引入GIS类问题
- VC新建目录CreateDirectory、得到exe存放路径GetModuleFileName、获取当前目录GetCurrentDirectory、CFileDialog设置打开默认路径
- Oracle Form在工具栏增加按钮
- SQL Server 2008 R2 性能计数器详细列表(二)
- 【eoeandroid社区索引】android AsyncTask汇总
- pthread_kill-----向线程发送信号