Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # coding: utf-8
- class Figure(object):
- def stretch_w(self, by):
- raise NotImplementedError()
- class Rectangle(Figure):
- def __init__(self, width, height):
- self.width = width
- self.height = height
- def stretch_w(self, by):
- self.width += by
- class Square(Rectangle):
- def __init__(self, side):
- self.side = side
- def _to_rect(self):
- new_rect = Rectangle(self.side, self.side)
- self.__class__ = new_rect.__class__
- self.__dict__ = new_rect.__dict__
- def stretch_w(self, by):
- self._to_rect()
- self.stretch_w(by)
- if __name__ == "__main__":
- fig = Square(5)
- print(type(fig), fig.__dict__)
- fig.stretch_w(5)
- print(type(fig), fig.__dict__)
- """
- >>> <class '__main__.Square'> {'side': 5}
- >>> <class '__main__.Rectangle'> {'width': 10, 'height': 5}
- """
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement