Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Counting recursion in a python program! [duplicate]

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.

like image 472
Mojo_Jojo Avatar asked Mar 26 '11 08:03

Mojo_Jojo


1 Answers

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...

like image 132
Jakob Bowyer Avatar answered Sep 19 '22 17:09

Jakob Bowyer