Call overridden method of parent class with child class object in python

后端 未结 5 782
遥遥无期
遥遥无期 2021-01-28 15:39

I have came around this question during an interview:

class Parent(object):
    def A(self):
        print \"in A\"
class Child(Parent):
    def A(self):
                


        
5条回答
  •  星月不相逢
    2021-01-28 16:23

    Use a super call passing the type of child class and the desired instance object which is just C:

    super(type(C), C).A() # of the form super(class, obj) where class == Child and obj == C
    

提交回复
热议问题