#!/usr/local/bin/python2 class ExtendableFunction(object): def __init__(self, func): self._base = func self._head = [] self._tail = [] def __call__(self, *args, **kwargs): for func in self._head: func(*args, **kwargs) result = self._base(*args, **kwargs) for func in self._tail: func(*args, **kwargs) return result @property def head(self): return self._head @property def tail(self): return self._tail @property def replica(self): def f(*args, **kwargs): for func in self._head: func(*args, **kwargs) result = self._base(*args, **kwargs) for func in self._tail: func(*args, **kwargs) return result return f def p(): print 123 e = ExtendableFunction(bool) e.head.append(p) print e(1) print e.replica(1)