Advertisement
Guest User

Untitled

a guest
Sep 20th, 2018
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.89 KB | None | 0 0
  1. # coding: utf-8
  2.  
  3.  
  4. class Figure(object):
  5.     def stretch_w(self, by):
  6.         raise NotImplementedError()
  7.  
  8.  
  9. class Rectangle(Figure):
  10.     def __init__(self, width, height):
  11.         self.width = width
  12.         self.height = height
  13.  
  14.     def stretch_w(self, by):
  15.         self.width += by
  16.  
  17.  
  18. class Square(Rectangle):
  19.     def __init__(self, side):
  20.         self.side = side
  21.  
  22.     def _to_rect(self):
  23.         new_rect = Rectangle(self.side, self.side)
  24.         self.__class__ = new_rect.__class__
  25.         self.__dict__ = new_rect.__dict__
  26.  
  27.     def stretch_w(self, by):
  28.         self._to_rect()
  29.         self.stretch_w(by)
  30.  
  31.  
  32. if __name__ == "__main__":
  33.     fig = Square(5)
  34.     print(type(fig), fig.__dict__)
  35.  
  36.     fig.stretch_w(5)
  37.     print(type(fig), fig.__dict__)
  38.  
  39.     """
  40.    >>> <class '__main__.Square'> {'side': 5}
  41.    >>> <class '__main__.Rectangle'> {'width': 10, 'height': 5}
  42.     """
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement