Skip to content

Instantly share code, notes, and snippets.

@datavudeja
Forked from fmder/count_calls.py
Created August 13, 2025 15:40
Show Gist options
  • Save datavudeja/3e3d2d480e345bfe943d3627c3824cb6 to your computer and use it in GitHub Desktop.
Save datavudeja/3e3d2d480e345bfe943d3627c3824cb6 to your computer and use it in GitHub Desktop.
Count function calls decorator
class countCalls(object):
"""Decorator that keeps track of the number of times a function is called.
::
>>> @countCalls
... def foo():
... return "spam"
...
>>> for _ in range(10)
... foo()
...
>>> foo.count()
10
>>> countCalls.counts()
{'foo': 10}
Found in the Pythod Decorator Library from http://wiki.python.org/moin web site.
"""
instances = {}
def __init__(self, func):
self.func = func
self.numcalls = 0
countCalls.instances[f] = self
def __call__(self, *args, **kwargs):
self.numcalls += 1
return self.func(*args, **kwargs)
def count(self):
"Return the number of times this function was called."
return countCalls.instances[self.func].numcalls
@staticmethod
def counts():
"Return a dict of {function: # of calls} for all registered functions."
return dict([(func.__name__, countCalls.instances[func].numcalls) for func in countCalls.instances])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment