fork download
import math

def caching(func):
    cache = {}
    def wrapped_func(*args):
        argstr = '({})'.format(', '.join(map(str, args)))
        if args not in cache:
            print(f'> calculating {func.__name__}{argstr}')
            cache[args] = func(*args)
        else:
            print(f'> reusing cached result for {func.__name__}{argstr}')
        return cache[args]
    return wrapped_func

@caching
def my_multiply(x, y):
    return x * y

@caching
def my_divide(x, y):
    return x / y

@caching
def my_hypot(a, b):
    return math.sqrt(a * a + b * b)

print(my_multiply(2, 2))
print(my_divide(2, 2))
print(my_multiply(2, 2))
print(my_divide(2, 2))
print(my_hypot(2, 3))
print(my_divide(2, 3))
print(my_hypot(2, 3))
print(my_hypot(3, 3))
Success #stdin #stdout 0.02s 9284KB
stdin
Standard input is empty
stdout
> calculating my_multiply(2, 2)
4
> calculating my_divide(2, 2)
1.0
> reusing cached result for my_multiply(2, 2)
4
> reusing cached result for my_divide(2, 2)
1.0
> calculating my_hypot(2, 3)
3.605551275463989
> calculating my_divide(2, 3)
0.6666666666666666
> reusing cached result for my_hypot(2, 3)
3.605551275463989
> calculating my_hypot(3, 3)
4.242640687119285