fork download
  1. import functools
  2. import types
  3.  
  4.  
  5. def function_decorator(f):
  6.  
  7. @functools.wraps(f)
  8. def wrapper(*args, **kwargs):
  9.  
  10. print("wrapper", f, args, kwargs)
  11. return f(*args, **kwargs)
  12.  
  13. return wrapper
  14.  
  15.  
  16. def class_decorator(cls):
  17.  
  18. for name in dir(cls):
  19.  
  20. attr = getattr(cls, name)
  21. if isinstance(attr, types.FunctionType):
  22.  
  23. setattr(cls, name, function_decorator(attr))
  24.  
  25. return cls
  26.  
  27.  
  28. @class_decorator
  29. class Yoba:
  30.  
  31. x = 1
  32. y = 2
  33.  
  34. def __init__(self):
  35.  
  36. print("Yoba.__init__")
  37.  
  38. def foo(self):
  39.  
  40. print("Yoba.foo")
  41.  
  42. def bar(self):
  43.  
  44. print("Yoba.bar")
  45.  
  46.  
  47. yoba = Yoba()
  48. yoba.foo()
  49. yoba.bar()
  50.  
Success #stdin #stdout 0.03s 10320KB
stdin
Standard input is empty
stdout
wrapper <function Yoba.__init__ at 0xb731e5cc> (<__main__.Yoba object at 0xb731de6c>,) {}
Yoba.__init__
wrapper <function Yoba.foo at 0xb731e6ec> (<__main__.Yoba object at 0xb731de6c>,) {}
Yoba.foo
wrapper <function Yoba.bar at 0xb72daa4c> (<__main__.Yoba object at 0xb731de6c>,) {}
Yoba.bar