Attributes of an instance can be changed (or created) by simply assigning them a value. However, if other attributes depend on the one just changed, it is desirable to change them simultaneously.
To demonstrate this, we consider an example: let's define a class that defines an object for planar triangles from three given points. A first attempt to set up such a class could be as follows:
class Triangle: def __init__(self, A, B, C): self.A = array(A) self.B = array(B) self.C = array(C) self.a = self.C - self.B self.b = self.C - self.A self.c = self.B - self.A def area(self): return abs(cross(self.b, self.c)) / 2
An instance of this triangle is created by this:
tr = Triangle([0., 0.], [1., 0.], [0., 1.])
Then its area is computed by calling the corresponding method:
tr.area() # returns 0.5
If we change an attribute, say point B, the corresponding edges a and c are not automatically...