'Is it possible to call 2nd parent class method by using child class object reference inside python in case of inheritance? If yes then how?

Suppose we have two class with different names but with the same name method in both class with no arguments.

For example:

class Parent_1: # define parent class
    def myMethod(self):
        print 'Calling parent method_1'

Another is:

class Parent_2: # define parent class
    def myMethod(self):
        print 'Calling parent method_2'

I have another class (child class) which inherit these both classes.

class Child(Parent_1, Parent_2): # define child class
    print "abc"
    #Parent_1().myMethod();
    #Parent_2().myMethod();

See here, if I try to call 2nd class method then I can call with the 2nd parent class reference inside child class. But when I am trying to call from outside by using child class object reference.

c = Child()
c.myMethod()

Output is:

abc
Calling parent method_1

Here, you can watch it will call 1st parent class method by default using child class reference.

What if I want to call same method of another class using child class reference explicitly without changing inherited base classes order?

Is it possible or not ? If yes, then how ?



Solution 1:[1]

You can call the unbound function and pass the self parameter explicitly:

Parent_2.myMethod(c)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Aran-Fey