Last active
May 28, 2020 07:31
-
-
Save czheo/851a9f35056f3a3fd5ea9cbd03a7df2f to your computer and use it in GitHub Desktop.
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
class Point: | |
def __init__(self, x, y): | |
self.x = x | |
self.y = y | |
def __repr__(self): | |
return "({}, {})".format(self.x, self.y) | |
def __add__(self, other): | |
def generator(): | |
return Point(self.x + other.x, self.y + other.y) | |
return DependentPoint(generator) | |
def __sub__(self, other): | |
def generator(): | |
return Point(self.x - other.x, self.y - other.y) | |
return DependentPoint(generator) | |
class DependentPoint(Point): | |
def __init__(self, generator): | |
self.generator = generator | |
@property | |
def x(self): | |
return self.generator().x | |
@property | |
def y(self): | |
return self.generator().y | |
a = Point(1, 2) | |
b = Point(3, 4) | |
c = Point(5, 6) | |
d = a + b - c | |
print(d) # (-1, 0) | |
a.x = 3 | |
print(d) # (1, 0) | |
c.y = 1 | |
print(d) # (1, 5) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment