Last active
December 17, 2015 22:56
-
-
Save vinovator/c7e1052c9dfbcc36f38d to your computer and use it in GitHub Desktop.
A simple decorator example in python
This file contains hidden or 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
# python 2.7.6 | |
# myDecorator.py | |
""" | |
A simple decorator example in python | |
""" | |
class SimpleClass: | |
""" | |
A simple class with add and subtract methods, undecorated | |
""" | |
def __init__(self, num1, num2): | |
""" | |
Arguments initialization | |
""" | |
self.num1 = num1 | |
self.num2 = num2 | |
def add(self): | |
return self.num1 + self.num2 | |
def sub(self): | |
return self.num1 - self.num2 | |
class DecoratedClass: | |
""" | |
A simple class with add and subtract methods, decorated | |
""" | |
def __init__(self, num1, num2): | |
""" | |
Arguments initialization | |
""" | |
self.num1 = num1 | |
self.num2 = num2 | |
def floatify(func): | |
""" | |
A decorator takes a function as input and returns a function as output | |
""" | |
def decorated_func(*args, **kwargs): | |
return float(func(*args, **kwargs)) | |
return decorated_func | |
@floatify | |
def add(self): | |
return self.num1 + self.num2 | |
@floatify | |
def sub(self): | |
return self.num1 - self.num2 | |
if __name__ == "__main__": | |
cls = SimpleClass(2,1) | |
print(cls.add(), cls.sub()) | |
dcls = DecoratedClass(2,1) | |
print(dcls.add(), dcls.sub()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment