简体   繁体   中英

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?

I'm recently started exploring python technology. I found a problem when I doing my exercise.

Suppose we have two class with different name but with same name method is exist in both class with no arguments. 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'm 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 wanna 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 ?

Thanks in advanced & please give me your valuable answers regarding this question and suggest me if I forgot to mention anything here. Best suggestion or answer will be appreciated.

您可以调用unbound函数并显式传递self参数:

Parent_2.myMethod(c)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM