decorator.py 507 B

12345678910111213141516171819202122
  1. # http://stackoverflow.com/questions/3118929/implementing-the-decorator-pattern-in-python
  2. class foo(object):
  3. def f1(self):
  4. print("original f1")
  5. def f2(self):
  6. print("original f2")
  7. class foo_decorator(object):
  8. def __init__(self, decoratee):
  9. self._decoratee = decoratee
  10. def f1(self):
  11. print("decorated f1")
  12. self._decoratee.f1()
  13. def __getattr__(self, name):
  14. return getattr(self._decoratee, name)
  15. u = foo()
  16. v = foo_decorator(u)
  17. v.f1()
  18. v.f2()