Assign attributes when another is changed

python

Solution

You can define `x` and `y` as properties:

@property
def x(self):
    return self.datum[0]

@x.setter
def x(self, value):
    self.datum[0] = value

@property
def y(self):
    return self.datum[1]

@y.setter
def y(self, value):
    self.datum[1] = value

This way, `x` and `y` are not attributes (and thus the data is stored only once in the `datum`), but stay usable as such (i.e.: `my_shape.x` and `my_shape.y` works as expected).

Problem

I have a Python Shape class with an attribute called datum. It is a list with two numbers, x and y. If I do ``` my_shape = Shape() ``` Then the datum is given default value: [0,0] Now, I want to reassign the datum by doing: ``` my_shape.datum = [3,2] ``` then my datum is now assigned that list. But I also have attributes x and y. How do I make x and y automatically update to become the first and second item of the datum list? I put this in the init ``` self.x = self.datum[0] self.y = self.datum[1] ``` But that only assigns x and y to the initialized values and I can't figure out how to update when self.datum updates. I'm sorry if this is confusing. Thank you so much for helping this newbie. -Jason

Original source