Skip to content

Instantly share code, notes, and snippets.

@czheo
Last active May 28, 2020 07:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save czheo/851a9f35056f3a3fd5ea9cbd03a7df2f to your computer and use it in GitHub Desktop.
Save czheo/851a9f35056f3a3fd5ea9cbd03a7df2f to your computer and use it in GitHub Desktop.
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