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

前端 未结 11 1052
情话喂你
情话喂你 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:25

    counter = 0
    
    def pop():
      counter += 1
      print counter
      #other function code
    
    a = [1,2,3,4]  
    a.pop()
    

    this should solve your issue and you should be able to see whats being counted. + every time you call the function the counter is going to be increased and printed with every pass of the function.

    IF ITS BUILT IN:

        counter = 0
        def newfunction():
          a = [1,2,3,4]  
          a.pop()
          counter += 1
          print counter
    

    the logic in this is that it will call your new function go into the function that is premade then step out of the built in function and then go on to mark the counter as increased. the output your counter.

提交回复
热议问题