Advertisement
lessientelrunya

Decorating a method

Apr 6th, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.49 KB | None | 0 0
  1. def method_friendly_decorator(method_to_decorate):
  2.     def wrapper(self, lie):
  3.         lie = lie - 3 # very friendly, decrease age even more :-)
  4.         return method_to_decorate(self, lie)
  5.     return wrapper
  6.  
  7.  
  8. class Laurel(object):
  9.  
  10.     def __init__(self):
  11.         self.age = 32
  12.  
  13.     @method_friendly_decorator
  14.     def sayYourAge(self, lie):
  15.         print("I am {0}, what did you think?".format(self.age + lie))
  16.  
  17. l = Laurel()
  18. l.sayYourAge(-3)
  19. #outputs: I am 26, what did you think?
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement