Created
April 29, 2013 06:41
-
-
Save sidchilling/5480047 to your computer and use it in GitHub Desktop.
Script to show how to decorate a method in Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Decorated methods is same as decorating functions. Just take 'self' into consideration | |
def method_friendly_decorator(method_to_decorate): | |
def wrapper(self, lie): | |
lie = lie + 3 | |
return method_to_decorate(self, lie) | |
return wrapper | |
class Hermione(object): | |
def __init__(self): | |
self.age = 19 | |
@method_friendly_decorator | |
def say_your_age(self, lie): | |
return 'I am %s years old' %(self.age - lie) | |
h = Hermione() | |
print h.say_your_age(lie = 2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment