December 2016
Intermediate to advanced
332 pages
6h 2m
English
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 these simultaneously:
Let us consider 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)) / 2An instance of this triangle is created by this:
tr = Triangle([0., 0.], [1., 0.], [0., ...
Read now
Unlock full access