daily pastebin goal
32%
SHARE
TWEET

Untitled

a guest Sep 20th, 2018 171 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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.     """
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top