0

Unable to call both functions from two different parent classes in Child class in Python 3.7.2

I tried calling default constructor as well as user defined functions It is just calling the first class function from the parameter list. When calling (Parent1, Parent2) it calls Parent1 function, when calling (Parent2, Parent1) it calls Parent2 function.

class Parent1():
    def fun1(self):
        print("Fun1 from Parent1")
    def fun2(self):
        print("Fun2 from Parent1")
class Child1(Parent1):
    def fun2(self):
        print("Fun2 from Child1")
obj1 = Child1()
obj1.fun1()
obj1.fun2()
print("========Block========")
class Parent2():
    def fun1(self):
        print("Fun1 from Parent2")
class Child2(Parent1, Parent2):
    def fun1(self):
        super().fun1()
        print("Fun1 from Child2")
obj2 = Child2()
obj2.fun1()
print("========Block========")
class Child3(Parent2, Parent1):
    def fun1(self):
        super().fun1()
        print("Fun1 from Child3")
obj3 = Child3()
obj3.fun1()
print("========Block========")
class First(object):
    def __init__(self):
        super(First, self).__init__()
        print("First")
class Second(object):
    def __init__(self):
        super(Second, self).__init__()
        print("Second")
class Third(object):
    def __init__(self):
        super(Third, self).__init__()
        print("Third")
Third()
sanyassh
  • 8,100
  • 13
  • 36
  • 70
Vijayant
  • 57
  • 1
  • 1
  • 5
  • Check out method resolution order http://python-history.blogspot.com/2010/06/method-resolution-order.html – bison Apr 04 '19 at 15:13
  • 2
    Possible duplicate of [python multiple inheritance from different paths with same method name](https://stackoverflow.com/questions/3810410/python-multiple-inheritance-from-different-paths-with-same-method-name) – bison Apr 04 '19 at 15:14
  • Check this link may be this will be use for you https://stackoverflow.com/questions/41356378/python-multiple-inheritance-calling-base-class-function/41356601 – AkhilKrishnan Apr 04 '19 at 15:39

1 Answers1

2

You can refer this link also

  class Parent1():
        def fun1(self):
            print("Fun1 from Parent1")
        def fun2(self):
            print("Fun2 from Parent1")

    class Child1(Parent1):
        def fun1(self):        
            Parent1.fun1(self)
            print("Fun2 from Child1")

    class Parent2():
        def fun1(self):
            print("Fun1 from Parent2")

    class Child2(Parent1, Parent2):
        def fun1(self):
            Parent1.fun1(self)
            Parent2.fun1(self)
            print("Fun1 from Child2")

    print("========Block========")
    obj1 = Child1()
    obj1.fun1()
    obj1.fun2()
    print("========Block========")    
    obj2 = Child2()
    obj2.fun1()
    print("========Block========")

o/p

========Block========
Fun1 from Parent1
Fun2 from Child1
Fun2 from Parent1
========Block========
Fun1 from Parent1
Fun1 from Parent2
Fun1 from Child2
========Block========
AkhilKrishnan
  • 524
  • 5
  • 17