How to track the number of recursive calls without using global variables in Python. For example, how to modify the following function to keep track the number of calls?
def f(n):
if n == 1:
return 1
else:
return n * f(n-1)
print f(5)
As delnan said, if you want all calls ever, it’s impossible without a global, so I’m assuming you just want the call depth, for which you just need to add a return value
If you’re dealing with several recursive calls, you can do a
max(call_depth1, call_depth2)(depth of longest call tree) or just sum the two (total number of actual calls)