How to call super method from grandchild class?

非 Y 不嫁゛ 提交于 2019-11-27 21:49:42

Well, this is one way of doing it:

class Grandparent(object):
    def my_method(self):
        print "Grandparent"

class Parent(Grandparent):
    def my_method(self):
        print "Parent"

class Child(Parent):
    def my_method(self):
        print "Hello Grandparent"
        Grandparent.my_method(self)

Maybe not what you want, but it's the best python has unless I'm mistaken. What you're asking sounds anti-pythonic and you'd have to explain why you're doing it for us to give you the happy python way of doing things.

Another example, maybe what you want (from your comments):

class Grandparent(object):
    def my_method(self):
        print "Grandparent"

class Parent(Grandparent):
    def some_other_method(self):
        print "Parent"

class Child(Parent):
    def my_method(self):
        print "Hello Grandparent"
        super(Child, self).my_method()

As you can see, Parent doesn't implement my_method but Child can still use super to get at the method that Parent "sees", i.e. Grandparent's my_method.

This works for me:

class Grandparent(object):
    def my_method(self):
        print "Grandparent"

class Parent(Grandparent):
    def my_method(self):
        print "Parent"

class Child(Parent):
    def my_method(self):
        print "Hello Grandparent"
        super(Parent, self).my_method()

Made and tested in python 3

class Vehicle:

# Initializer / Instance Attributes
def __init__(self, name, price):
    self.name = name
    self.price = price

# instance's methods
def description(self):
    print("\nThe car {} has a price of {} eur".format(self.name, self.price))
#Object Vehicle

 m3 = Vehicle("BMW M3", 40000)

 m3.description()

 class Camper(Vehicle):

 def __init__(self,nome,prezzo,mq):
     super().__init__(nome,prezzo)
     self.mq=mq

     # instance's methods

 def description(self):
     super().description()
     print("It has a dimension of",format(self.mq)+" mq")

 #Camper Object(A camper is also a Vehicle)
 marcopolo=Camper("Mercede MarcoPolo",80000,15)

 marcopolo.description()

Output:
The car BMW M3 has a price of 40000 eur
The car Mercede MarcoPolo has a price of 80000 eur
It has a dimension of 15 mq

You can do this by following ways

class Grandparent(object):
    def my_method(self):
        print "Grandparent"

class Parent(Grandparent):
    def my_other_method(self):
        print "Parent"

class Child(Parent):
    def my_method(self):
        print "Inside Child"
        super(Child, self).my_method()

In this case Child will call base class my_method but base class my_method is not present there so it will call base class of parent class my_method in this way we can call my_method function of grandparent

Another Way

class Grandparent(object):
    def my_method(self):
        print "Grandparent"

class Parent(Grandparent):
    def my_other_method(self):
        print "Parent"

class Child(Parent):
    def my_method(self):
        print "Inside Child"
        super(Parent, self).my_method()

In this way we are directly calling function base class my_method function of the parent class

Another way but not pythonic way

class Grandparent(object):
    def my_method(self):
        print "Grandparent"

class Parent(Grandparent):
    def my_other_method(self):
        print "Parent"

class Child(Parent):
    def my_method(self):
        print "Inside Child"
        Grandparent.my_method()

In this way we are directly calling my_method function by specifying the class name.

If you want two levels up, why not just do

class GrandParent(object):                                                       

    def act(self):                                                               
        print 'grandpa act'                                                      

class Parent(GrandParent):                                                       

    def act(self):                                                               
        print 'parent act'                                                       

class Child(Parent):                                                             

    def act(self):                                                               
        super(Child.__bases__[0], self).act()                                    
        print 'child act'                                                        


instance = Child()                                                               
instance.act()

# Prints out
# >>> grandpa act
# >>> child act      

You can add something defensive like checking if __bases__ is empty or looping over it if your middle classes have multiple inheritance. Nesting super doesn't work because the type of super isn't the parent type.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!