在python程序中计算递归! [英] Counting recursion in a python program!
本文介绍了在python程序中计算递归!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要计算python程序中递归的次数.所以基本上我需要一个静态变量类型的东西(比如在 C 中)它可以计算函数被调用的次数.
I need to count the number of times recursion in a python program. So basically I need a static variable kind of thing (like in C) which can count the number of times the function is called.
推荐答案
只需通过递归传递一个计数器
Just pass a counter with the recursion
def recur(n, count=0):
if n == 0:
return "Finished count %s" % count
return recur(n-1, count+1)
或者我确定有一些花哨的装饰器,我现在要去调查......
Or im sure there is some fancy decorator, Im gonna investigate that now...
这篇关于在python程序中计算递归!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文