scientific-programming-exer.../ex_35.py

29 lines
467 B
Python

import time
class counter_of_calls(object):
def __init__(self, f):
self._f = f
self._cnt = 0
def __call__(self, *args, **kwargs):
self._cnt += 1
result = self._f(*args, **kwargs)
print(self._f.__name__, "has been called", self._cnt, "times; last time:", time.time())
return result
@counter_of_calls
def f(x):
return x + 6
@counter_of_calls
def g(x):
return x + 7
if( __name__ == "__main__"):
print(f(3))
print(f(5))
print(g(3))
print(g(5))