How to call super method from grandchild class?

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.


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.


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