Just learn a new python feature called @property. Let's say we create a class called Circle as follows.
class Square: side = 2 area = side * side s = Square()
Now:
>>> s.side 2 >>> s.area 4
However:
>>> s.side = 3 >>> s.side 3 >>> s.area 4
Not cool! How to solve that?
class Square: side = 2 @property def area(self) return self.side * self.side s = Square()
See now.
>>> s.side = 3 >>> s.side 3 >>> s.area 9
No comments:
Post a Comment