slots_2.py 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. class Rectangle:
  2. __slots__ = '__width', 'height'
  3. def __init__(self, a, b):
  4. self.width = a
  5. self.height = b
  6. @property
  7. def width(self):
  8. return self.__width
  9. @width.setter
  10. def width(self, value):
  11. print("Setter called")
  12. self.__width = value
  13. @property
  14. def perimetr(self):
  15. return (self.height + self.width) * 2
  16. @property
  17. def area(self):
  18. return self.height * self.width
  19. # У класса наследника будет атрибут __dict__ если не задать атрибут __slots__
  20. class Square(Rectangle):
  21. # этот slots расширяет имена родительского класса
  22. __slots__ = 'color'
  23. def __init__(self, a, b, color):
  24. super().__init__(a, b)
  25. self.color = color
  26. def main():
  27. a = Rectangle(3, 4)
  28. b = Rectangle(5, 6)
  29. print(b.perimetr, b.area)
  30. s = Square(3, 6, 'red')
  31. # print(s.__dict__)
  32. if __name__ == '__main__':
  33. main()