这是Python多层继承的一个特例,祖父,父亲,儿子都有 draw 方法,那么经过多次继承后,如何用一种通用的方法给不同层次的方法传递参数,特别是变长的,不定长度的参数。
class Root:
def draw(self,**kwds):
# the delegation chain stops here
print('Root Drawing draw ')
assert not hasattr(super(), 'draw')
class Shape(Root):
def __init__(self, **kwds):
self.shapename = kwds['shapename']
# print('Shape class',kwds)
# super().__init__(**kwds)
def draw(self):
print('Drawing. Setting shape to:', self.shapename)
super().draw()
class ColoredShape(Shape):
def __init__(self, **kwds):
self.color = kwds['color']
# print('ColoredShape class',kwds)
super().__init__(**kwds)
def draw(self):
print('Drawing. Setting color to:', self.color)
super().draw()
input_kwds={'color':'blue', 'shapename':'square'}
cs = ColoredShape(**input_kwds)
cs.draw()
Drawing. Setting color to: blue
Drawing. Setting shape to: square
Root Drawing draw