class Root(object): def draw(self): # the delegation chain stops here print('Root draw called') assert not hasattr(super(Root, self), 'draw') class Moveable(object): def __init__(self, x, y): self.x = x self.y = y def draw(self): print('Drawing. At position x={}, y={}'.format(self.x, self.y)) class MoveableAdaptor(Root): def __init__(self, x, y, **kwds): self.moveable = Moveable(x,y) super(MoveableAdaptor, self).__init__(**kwds) def draw(self): self.moveable.draw() super(MoveableAdaptor, self).draw() class CraftedShape(Root): def __init__(self, craftname, **kwds): self.craftname = craftname super(CraftedShape, self).__init__(**kwds) def draw(self): print('Drawing. Crafting shape to:', self.craftname) super(CraftedShape, self).draw() class Shape(Root): def __init__(self, shapename, **kwds): self.shapename = shapename super(Shape, self).__init__(**kwds) def draw(self): print('Drawing. Setting shape to:', self.shapename) super(Shape, self).draw() class ColoredShape(Shape, CraftedShape, MoveableAdaptor): def __init__(self, color, **kwds): self.color = color super(ColoredShape, self).__init__(**kwds) def draw(self): print('Drawing. Setting color to:', self.color) super(ColoredShape, self).draw() cs = ColoredShape(color='blue', shapename='square', craftname='umbrella', x=9, y=10) cs.draw()
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question