開(kāi)啟尾遞歸優(yōu)化后(tail_call_optimized裝飾器)的調(diào)用棧
通過(guò)pudb右邊欄的stack, 可以很清晰的看到調(diào)用棧的變化.
因?yàn)槲策f歸沒(méi)有調(diào)用棧的嵌套, 所以Python也不會(huì)報(bào) RuntimeError: maximum recursion depth exceeded
錯(cuò)誤了!
這里解釋一下 sys._getframe() 函數(shù):
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深度調(diào)用的棧幀對(duì)象.import sysdef get_cur_info(): print sys._getframe().f_code.co_filename # 當(dāng)前文件名 print sys._getframe().f_code.co_name # 當(dāng)前函數(shù)名 print sys._getframe().f_lineno # 當(dāng)前行號(hào) print sys._getframe().f_back # 調(diào)用者的幀
更多關(guān)于 sys._getframe
請(qǐng)看 Frame Hacks