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

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

    This doesn't work for builtin functions, but an interesting approach would be:

    def myfunction():
        myfunction.counter += 1
    myfunction.counter = 0
    

    You're giving the function an attribute, so every call that attribute is updated. No global variables needed.

    Built-ins are read-only. They cannot be modified.

    0 讨论(0)
  • 2020-11-28 09:01

    Just define a global variable and increment it inside function.

    a = 0
    def some_function():
        global a
        a+=1
        <..Your code.>
    

    This will automatically be incremented as function is used and you can access it globally.

    0 讨论(0)
  • 2020-11-28 09:07

    You could use a decorator that tracks how many times the function is called. Since list is a built-in, you can't decorate or replace its pop method so you'd have to use your own list class, for example.

    def counted(f):
        def wrapped(*args, **kwargs):
            wrapped.calls += 1
            return f(*args, **kwargs)
        wrapped.calls = 0
        return wrapped
    
    class MyList(list):
        @counted
        def pop(self, *args, **kwargs):
            return list.pop(self, *args, **kwargs)
    
    x = MyList([1, 2, 3, 4, 5])
    for i in range(3):
        x.pop()
    
    print x.pop.calls # prints 3
    
    0 讨论(0)
  • 2020-11-28 09:07

    Just define a global statement in your function.

    count = 1
    def your_func():
      global count
      print(count)
      count= count +1
    
    0 讨论(0)
  • 2020-11-28 09:14

    One approach is to create a proxy of the instance for which you want to count attribute access:

    from collections import Counter
    
    class CountingProxy():
        def __init__(self, instance):
            self._instance = instance
            self.count = Counter()
    
        def __getattr__(self, key):
            if hasattr(self._instance, key):
                self.count[key] += 1
            return getattr(self._instance, key)
    
    
    >>> l = [1,2,3,4,5]
    >>> cl = CountingProxy(l)
    >>> cl.pop()
    5
    >>> cl.append(10)
    >>> cl.index(3)
    2
    >>> cl.reverse()
    >>> cl.reverse()
    >>> cl.count
    Counter({'reverse': 2, 'pop': 1, 'append': 1, 'index': 1})
    
    0 讨论(0)
  • 2020-11-28 09:15

    I did it copying the way JavaScript console.count() method works. That's my code:

    class Terminal(object):
        __count_names = []
        def count(self, name='default'):
            # check if name already exists
            i = next((self.__count_names.index(item) for item in self.__count_names if item['name'] == name), None)
            # if name argument does not exist register it
            if i is None:
                dictionary = { 'name': name, 'count': 1 }
                self.__count_names.append(dictionary)
            # if exists increment 'count'
            else:
                dictionary = self.__count_names[i]
                dictionary['count'] += 1
                self.__count_names[i] = dictionary
            # finally print name and count
            print(f"{dictionary['name']} : {dictionary['count']}")
    

    Your code should look like this:

    terminal = Terminal()
    def myFunc():
        terminal.count("myFunc")
    
    myFunc()
    myFunc()
    myFunc("myFunc")
    

    Output:

    myFunc: 1
    myFunc: 2
    myFunc: 3
    myFunc: 4
    
    0 讨论(0)
提交回复
热议问题