How to call super method from grandchild class?

PythonPython 2.7InheritanceSuperGrandchild

Python Problem Overview


I am working with some code that has 3 levels of class inheritance. From the lowest level derived class, what is the syntax for calling a method 2 levels up the hierarchy, e.g. a super.super call? The "middle" class does not implement the method I need to call.

Python Solutions


Solution 1 - Python

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.

Solution 2 - Python

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()

Solution 3 - Python

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.

Solution 4 - Python

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.

Solution 5 - Python

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSeanLabsView Question on Stackoverflow
Solution 1 - PythonCrazyCastaView Answer on Stackoverflow
Solution 2 - PythonTomaszView Answer on Stackoverflow
Solution 3 - PythonIamnotBatmanView Answer on Stackoverflow
Solution 4 - PythonAnand TripathiView Answer on Stackoverflow
Solution 5 - PythonLucianoDemuruView Answer on Stackoverflow