Advertisement
Guest User

Untitled

a guest
Jan 18th, 2017
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.52 KB | None | 0 0
  1. def aop(cls):
  2. class Wrapper:
  3. def __init__(self, *args):
  4. self.wrapped = cls(*args) # create instance of decorated class
  5. def __getattr__(self, name):
  6. print('Getting the {} of {}'.format(name, self.wrapped))
  7. return getattr(self.wrapped, name)
  8. return Wrapper
  9.  
  10. @aop
  11. class Foo:
  12. def test_foo(self):
  13. print("foo called")
  14. def test_bar(self):
  15. print("bar called")
  16.  
  17. f = Foo()
  18. f.test_foo()
  19.  
  20. # Getting the test_foo of <__main__.Foo object at 0x1099c36a0>
  21. # foo called
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement