Skip to content

Instantly share code, notes, and snippets.

@luliangce
Last active May 28, 2020 08:34
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 luliangce/5c1c84392a51c02cb90b62e173d12ed0 to your computer and use it in GitHub Desktop.
Save luliangce/5c1c84392a51c02cb90b62e173d12ed0 to your computer and use it in GitHub Desktop.
...
class Point:
ref = None
operator = None
def __init__(self, x, y):
self._x = x
self._y = y
@property
def x(self):
if self.ref:
return getattr(self, self.operator)(self.ref[0].x, self.ref[1].x)
return self._x
@property
def y(self):
if self.ref:
return getattr(self, self.operator)(self.ref[0].y, self.ref[1].y)
return self._y
@x.setter
def x(self, v):
self._x = v
self._y = self.y
self.ref = None
@y.setter
def y(self, v):
self._y = v
self._x = self.x
self.ref = None
def __add__(self, other: "Point") -> "Point":
point = Point(0, 0)
point.ref = (self, other)
point.operator = "_add"
return point
@staticmethod
def _add(x, y):
return x + y
def __repr__(self):
return f'{self.x},{self.y}'
A = Point(1, 2)
B = Point(1, 3)
C = A + B
print(C)
A.x = 0
print(C)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment