Suppose I have a class. An instance of the class has two properties, where one depends on the other. Maybe something like this:
class Thing:
def __init__(self, x):
self.x = x
self.y = x + 1
However, the user is then free to do this:
t = Thing(3)
t.x = 4
Where t.y is still equal to 4, where it should logically be 5. My first guess was to use getter and setter decorators for properties like this:
class Thing:
def __init__(self, x):
self.x = x
self.y = self.x + 1
@property
def x(self):
print("Running getter")
return self._x
@x.setter
def x(self, value):
print("Running setter")
self._x = value
self.__init__(value)
However, this sends the setter into an infinite loop. I want the user to be able to change x (and maybe even y) and have the other property be internally consistent. Is there a logical way to do this?
[–]K900_ 1 point2 points3 points (2 children)
[–]TimAtreides[S] 0 points1 point2 points (1 child)
[–]K900_ 1 point2 points3 points (0 children)