Decorator
Function Decorator
#!/usr/bin/python

def func_decorator(func):
    def wrapper(x):
        if x == 0:
            raise Exception('Denominator is zero ...');
        return func(x);
    return wrapper;

@func_decorator
def inverse(x):
    return 1.0/x;

try:
    print inverse(10);
    print inverse(0);
except Exception, e:
    print e
		
Memoization
def memoize(f):
    memo = {}
    def helper(x):
        if x not in memo:            
            memo[x] = f(x)
        return memo[x]
    return helper
    

def fib(n):
    if n == 0:
        return 0
    elif n == 1:
        return 1
    else:
        return fib(n-1) + fib(n-2)

fib = memoize(fib)

print(fib(40))
		
Class Decorator
class Fibonacci:
    def __init__(self):
        self.cache = {}
    def __call__(self, n):
        if n not in self.cache:
            if n == 0:
                self.cache[0] = 0
            elif n == 1:
                self.cache[1] = 1
            else:
                self.cache[n] = self.__call__(n-1) + self.__call__(n-2)
        return self.cache[n]

fib = Fibonacci()

for i in range(15):
    print(fib(i), end=", ")
		
Reference