class as decorator

Decorator Decorator is a callable which can modify the function, method, class on runtime. Most ofthe decorators uses closure but it is possible to use class. Closure import functools def cache(f): storage = {} @functools.wraps(f) def inner(n): value = storage.get(n) if value: print("Returning value from cache") return value value = f(n) storage[n] = value return value return inner @cache def factorial(n): if n <= 1: return 1 return n * factorial(n - 1) >>>factorial(20) 2432902008176640000 >>>factorial(20) Returning from cache 2432902008176640000 cache is a function which takes function as an argument and returns a function. [Read More]