How to override a method in a Python class

The class utility offered by the Python computer programming language, is a very important tool to the Python coder as it offers a way to develop custom extension classes which inherit attributes from a base one. By using the class utility offered by the Python computer programming language, the developer can easily get the most out of the object oriented programming concept.

Having used the Python class utility on my side projects, I truly understand how class objects can ease the process of software development and also help the programmer to do rapid prototyping.

By inheriting from a base class, there are two key things the coder accomplishes. He or she inherits all the attributes of the base class in the subclass, and also ends up with a custom class which can be customized by overriding methods.

What's the one thing every developer wants? More screens! Enhance your coding experience with an external monitor to increase screen real estate.

Before going any further through this article, make sure to launch a new Python interactive console in your own operating system, so you can practice the code samples by yourself.

Once you have managed to launch a new Python interactive shell on your own operating system, make sure to follow the Python syntax shown below to declare a base class.

class Base(object):
    
    def __init__(self, data):
        self.data = data

    def display(self):
        print(self.data)

The above Python class is a very simple one. At the moment it takes the argument data in its constructor and assigns it to the self.data attribute of the instance.

Now, to make things more concrete, create a new instance from the above class like shown in the following example.

b = Base('codetheory.in')

Once you have managed to create a fresh instance by following the piece of Python code shown above, you can easily make use of the new object like shown below.

b.data

Once the above piece of Python code gets executed in my interactive console, I get the following output.

'codetheory.in'

To print the above string object, part of the instance object which we have created, the display method defined in the class Base, can be also used. The syntax for making use of the display method, part of the Python instance called b, is shown below.

b.display()

Once the above piece of Python code is executed on my Python interactive console, I get the following output.

codetheory.in

The method display, defined in the Python class Base, is an instance’s utility. It makes use of the Python builtin statement called print, to print self.data on the console.

Since Python is an object oriented computer programming language, one can easily make use of a class to inherit from it. Taking in consideration that we have already defined a class object in our Python interactive console, we can easily make a subclass out of it by making use of the following syntax.

class SubBase(Base):
    pass  

Based on my personal experience with the Python computer programming language, when the coder defines a subclass, the object being created inherits all the attributes defined in the mother class.

To prove it yourself, create a new instance from the subclass which you defined according to the piece of Python code being shown above.

A fresh instance based on the subclass shown above, can be generated with the help of the following piece of Python code.

sub = SubBase('website')

As you can see from the above piece of Python code, the instance generated from the subclass SubBase takes a data argument too since it inherits the constructor defined in the mother class called Base.

Not only does the subclass SubBase inherit the __init__ constructor from its mother class called Base, but also the display method too. To make sure such claim is true, run the following piece of Python code in your interactive shell.

sub.display()

Once the above piece of Python code is executed on my interactive console, the following output is being displayed on the console.

website

As you can see from the above output, the subclass which has been generated from the Python class Base, has inherited all the attributes of its mother.

You can also access the data attribute of the instance object like shown in the following example.

sub.data

Once the above piece of Python code is executed in my interactive shell, the following string object is being displayed on the console.

'website'

Now, let’s create a new subclass from the mother class Base and redefine the method display with the main purpose of customizing it to our own desire and specific requirements.

Make sure to type the following piece of Python code in your interactive shell so you can declare a new subclass object based on the mother class Base which we have already created.

class SecondBase(Base):
    def display(self):
        print('The value of data is %s' % self.data) 

As you can see from the above piece of Python code, we have redefined the method in our new subclass with the main purpose of overriding the one in the mother class Base.

Now create a fresh instance object based on the subclass which we declared above, by following the syntax shown below.

second_base = SecondBase('custom display method')

Once the above object has been created, try to access its display method by following the piece of Python code being shown below.

second_base.display()

Once the above piece of Python code is executed in my interactive shell, the following result comes up on the console.

The value of data is custom display method

By looking at the above output, one can easily understand that the display method defined in the subclass SecondBase is totally different from the one found in the mother class Base.

Final thoughts

With the help of the class utility the Python coder can easily reuse and customize code without totally changing it. Through this article, the reader learned how to override methods in their Python class, by redefining them in their subclasses.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Leave a Reply

Your email address will not be published. Required fields are marked *