is there a way to track the number of times a function is called?

前端 未结 11 1107
情话喂你
情话喂你 2020-11-28 08:56

So i\'m trying to make a function that keeps track how many times a method is called. for example:

a = [1,2,3,4]
a.pop()

i want to know how

11条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-28 09:20

    i used the following little trick to track how many times the function was called

    def myfun(s,i=[0]):    
        print(s)    
        i[0]+=1 # mutable variable get evaluated ONCE
        return i[0]
    
    >>> myfun('aaa')
    aaa
    1
    >>> myfun('bbb')
    bbb
    2
    

提交回复
热议问题