#!/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): head = self._head[:] def f(*args, **kwargs): for func in head: func(*args, **kwargs) result = self._base(*args, **kwargs) for func in self._tail: func(*args, **kwargs) return result return f def p(*args): print 123 def q(*args): print 456 e = ExtendableFunction(bool) e.head.append(p) print e.replica(1) e.head.append(q) print e.replica(1)