Created
July 6, 2018 16:57
-
-
Save alysivji/3123c3b93621cda58cb0020eacdc05f8 to your computer and use it in GitHub Desktop.
Decorators inside of classes. ChiPy Slack question 20180706
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
def add_two(func): | |
def _wrapper(*args, **kwargs): | |
return func(*args, **kwargs) + 2 | |
return _wrapper | |
class Point: | |
def __init__(self, x, y): | |
self.x = x | |
self.y = y | |
def __repr__(self): | |
return f"<Point {self.x}, {self.y}>" | |
def double_x(self): | |
return self.x * 2 | |
@add_two | |
def double_x_plus_more(self): | |
return self.x * 2 | |
def add_two_inside_class(self, func): | |
def _wrapper(*args, **kwargs): | |
return func(*args, **kwargs) + 2 | |
return _wrapper | |
@add_two_inside_class | |
def double_x_plus_more_inside_class(self): | |
return self.x * 2 | |
if __name__ == "__main__": | |
p = Point(2, 5) | |
print(p.double_x_plus_more()) | |
print(p.double_x_plus_more_inside_class()) | |
print(p) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment