fork download
  1. class Square(object):
  2. def __init__(self, side, colour="black"):
  3. self.side = side
  4. self.colour = colour
  5.  
  6. @property
  7. def area(self):
  8. return self.side ** 2
  9.  
  10. @area.setter
  11. def area(self, value):
  12. self.side = value ** 0.5
  13.  
  14. @property
  15. def perimeter(self):
  16. return self.side * 4
  17.  
  18. @perimeter.setter
  19. def perimeter(self, value):
  20. self.side = value / 4.0
  21.  
  22. def __repr__(self):
  23. return "%s square with the side: %.2f, area: %.2f, perimeter: %.2f" % (
  24. self.colour.title(), self.side, self.area, self.perimeter)
  25.  
  26. square = Square(8)
  27. print(square)
  28.  
  29. square.side = 40
  30. print(square)
  31.  
  32. square.colour = "yellow"
  33. square.area = 89
  34. print(square)
  35.  
  36. square.perimeter = 134.6
  37. print(square)
Success #stdin #stdout 0.03s 9984KB
stdin
Standard input is empty
stdout
Black square with the side: 8.00, area: 64.00, perimeter: 32.00
Black square with the side: 40.00, area: 1600.00, perimeter: 160.00
Yellow square with the side: 9.43, area: 89.00, perimeter: 37.74
Yellow square with the side: 33.65, area: 1132.32, perimeter: 134.60