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 

see code running on ideone.com


Comments

Popular posts from this blog

javascript - Laravel datatable invalid JSON response -

java - Exception in thread "main" org.springframework.context.ApplicationContextException: Unable to start embedded container; -

sql server 2008 - My Sql Code Get An Error Of Msg 245, Level 16, State 1, Line 1 Conversion failed when converting the varchar value '8:45 AM' to data type int -