How to call super method from grandchild class?

前端 未结 5 640
无人共我
无人共我 2020-12-05 16:57

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,

5条回答
  •  难免孤独
    2020-12-05 17:43

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

提交回复
热议问题