SZanlongo
8/8/2016 - 3:54 PM

Adding extra functionality to parent class method From: https://stackoverflow.com/questions/37679421/adding-extra-functionality-to-parent-c

You can always call code from the parent using the super() function. It gives a reference to the parent. So, to call parent_method(), you should use super().parent_method().

Here's a code snippet (for python3) that shows how to use it.

class ParentClass: 
    def f(self): 
        print("Hi!"); 

class ChildClass(ParentClass): 
    def f(self):
        super().f(); 
        print("Hello!"); 

In python2, you need to call super with extra arguments: super(ChildClass, self). So, the snippet would become:

class ParentClass: 
    def f(self): 
        print("Hi!"); 

class ChildClass(ParentClass): 
    def f(self):
        super(ChildClass, self).f(); 
        print("Hello!"); 

If you call f() on an instance of ChildClass, it will show: "Hi! Hello!".

If you already coded in java, it's basically the same behaviour. You can call super wherever you want. In a method, in the init function, ...

There are also other ways to do that but it's less clean. For instance, you can do:

ParentClass.f(self) 

To call the f function of parent class.