Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding extra functionality to parent class method without changing its name [duplicate]

Tags:

python

class

I have two classes one parent and other child.

class Parent(object):

    def __init__(self):
        #does something

    def method_parent(self):
        print "Parent"

class Child(Parent):

    def __init__(self):
        Parent.__init__(self)

    def method_parent(self):
        print "Child"

After inheriting the parent I want to modify Parent method method_parent keeping the original functionality of that method and adding some extra lines of code to that method.

I know that I can create a new method like

def method_child(self):
    self.method_parent()
    #Add extra lines of code to perform something 

But I want to use the original name of method. I can't copy the source for that method because, the method is from a C module

what I want to achieve is something like this

def method_parent():
    #do parent_method stuff
    #do extra stuff

Is that even possible?

like image 512
Harwee Avatar asked Jun 07 '16 12:06

Harwee


People also ask

What does super () __ Init__ do?

Understanding Python super() with __init__() methods It is known as a constructor in Object-Oriented terminology. This method when called, allows the class to initialize the attributes of the class. The super() function allows us to avoid using the base class name explicitly.

Can you call parent class without its instance creation in Python?

Well this can done using Python. You just have to create an object of the child class and call the function of the parent class using dot(.) operator.

Can we call parent class method with child class object without overriding?

If you override a parent method in its child, child objects will always use the overridden version. But; you can use the keyword super to call the parent method, inside the body of the child method.

Can child class override the properties of parent class in Python?

If you add a method in the child class with the same name as a function in the parent class, the inheritance of the parent method will be overridden.


2 Answers

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 te 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.

like image 108
Alexis Clarembeau Avatar answered Oct 12 '22 23:10

Alexis Clarembeau


This is what the super function does.

class Child(Parent):

    def __init__(self):
        super(Child, self).__init__()

    def method_parent(self):
        super(Child, self).method_parent()
        print "Child"

In Python 3, you can call super without the arguments, like super().method_parent()

like image 36
Zah Avatar answered Oct 12 '22 23:10

Zah