Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

overwrite parent method partially in python

I have a situation when I need to overwrite parent method but only in one line. My initial code is quite big so I clarify what I mean in an example. I have method from parent:

class parent():
    def method(self):
        bla
        bla
        print("Parent method is working")
        bla
        bla

And child:

class child(parent):
    def method(self):
        bla
        bla
        print("Child method working")
        bla
        bla

As you can see, two methods are pretty much the same but one line is different. Do I have to write the same code in child method just to print different output or there is dark magic in python how to overwrite only one line?

like image 984
Влад Христенко Avatar asked Jan 29 '15 22:01

Влад Христенко


People also ask

How do you overwrite a parent in Python?

In Python method overriding occurs by simply defining in the child class a method with the same name of a method in the parent class. When you define a method in the object you make this latter able to satisfy that method call, so the implementations of its ancestors do not come in play.

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.

How do you overcome method overriding in Python?

This can generally be achieved by two ways. Using Classname: Parent's class methods can be called by using the Parent classname. method inside the overridden method. Using Super(): Python super() function provides us the facility to refer to the parent class explicitly.

Can you overwrite functions in Python?

Method overriding in Python is when you have two methods with the same name that each perform different tasks. This is an important feature of inheritance in Python. In method overriding, the child class can change its functions that are defined by its ancestral classes.


2 Answers

You can introduce an helper method that you override in the child.

class parent(object):
  def method(self):
    blah
    self.helper()
    blah
  def helper(self):
    print("parent running")

class child(parent):
  def helper(self):
    print("child running")
like image 148
Sylvain Defresne Avatar answered Oct 02 '22 04:10

Sylvain Defresne


There isn't a magic way to do so, you have to rewrite the entire method.

Now, of course, there are various workarounds. If you can edit the parent, then you can:

  1. Separate out the part you want to override in the child in a separate method to be re-implemented in the child.
  2. If feasible, pass an argument (or even use a class attribute) to the method to alter the method's behavior.

In your case, method #1 would look like:

class Parent(object):
     def method(self):
         print("Before!")
         self.inner()
         print("After!")

     def inner(self):
         print "In the parent"

And the child:

class Child(Parent):
    def inner(self):
        print("In the child")
like image 38
Thomas Orozco Avatar answered Oct 02 '22 04:10

Thomas Orozco