开启尾递归优化后(tail_call_optimized装饰器)的调用栈
通过pudb右边栏的stack, 可以很清晰的看到调用栈的变化.
因为尾递归没有调用栈的嵌套, 所以Python也不会报 RuntimeError: maximum recursion depth exceeded
错误了!
这里解释一下 sys._getframe() 函数:
sys._getframe([depth]):Return a frame object from the call stack.If optional integer depth is given, return the frame object that many calls below the top of the stack.If that is deeper than the call stack, ValueEfror is raised. The default for depth is zero,returning the frame at the top of the call stack.即返回depth深度调用的栈帧对象.import sysdef get_cur_info(): print sys._getframe().f_code.co_filename # 当前文件名 print sys._getframe().f_code.co_name # 当前函数名 print sys._getframe().f_lineno # 当前行号 print sys._getframe().f_back # 调用者的帧
更多关于 sys._getframe
请看 Frame Hacks