multiple inheritance in python with super -
this question has answer here:
class parent1(object): def foo(self): print "p1 foo" def bar(self): print "p1 bar" class parent2(object): def foo(self): print "p2 foo" def bar(self): print "p2 bar" class child(parent1, parent2): def foo(self): super(parent1, self).foo() def bar(self): super(parent2, self).bar() c = child() c.foo() c.bar()
the intention inherit foo() parent1 , bar() parent2. c.foo() resulting parent2 , c.bar() reulting error. please point problem , provide solution.
you call methods directly on parent classes, providing self
argument manually. should give highest level of control , easiest way understand. other points of view might suboptimal though.
here's child
class, rest of code stays same:
class child(parent1, parent2): def foo(self): parent1.foo(self) def bar(self): parent2.bar(self)
running snippet described changes results in desired output:
p1 foo p2 bar
Comments
Post a Comment