Method Overriding in Python – Examples

Python is a powerful, versatile language that programmers across the globe love. Today, we’re diving deep into one of its core features: method overriding. This post is perfect for experienced Python programmers seeking to fine-tune their understanding of method overriding and pick up some best practices.

A Refresher on Method Overriding

In Python, method overriding is a key aspect of Object-Oriented Programming (OOP). It occurs when a subclass, or derived class, has a method with the same name as a method in its superclass or base class. The subclass’s method ‘overrides’ the superclass’s method, allowing the subclass to provide a different implementation. We had written a short blog post earlier on this topic. You can read that here: Guide - Method Overriding in Python

Overriding in Action: An Example

Let’s start with a simple example of method overriding:

class Vehicle:
    def mileage(self):
        pass

class Car(Vehicle):
    def mileage(self):
        return "The mileage of this car is 30km/l"

In this example, the Car subclass overrides the mileage() method of the Vehicle superclass.

my_car = Car()
print(my_car.mileage())  # Outputs: The mileage of this car is 30km/l

Overriding Multiple Methods

A subclass can override multiple methods from its superclass. Consider the following example:

class Mammal:
    def diet(self):
        return "Omnivore"

    def habitat(self):
        return "Various habitats"

class Lion(Mammal):
    def diet(self):
        return "Carnivore"

    def habitat(self):
        return "Grasslands"

Here, the Lion subclass overrides both the diet() and habitat() methods from the Mammal superclass.

simba = Lion()
print(simba.diet())     # Outputs: Carnivore
print(simba.habitat())  # Outputs: Grasslands

Overriding with super()

Python’s super() function lets you call methods from the superclass within the subclass. This is useful when you want to extend, rather than entirely replace, the superclass’s method.

class Bird:
    def diet(self):
        return "This bird's diet is"

class Sparrow(Bird):
    def diet(self):
        return super().diet() + " seeds and insects"

In this case, the Sparrow subclass extends the diet() method of the Bird superclass.

jack = Sparrow()
print(jack.diet())  # Outputs: This bird's diet is seeds and insects

Best Practices with Method Overriding

  1. Ensure method compatibility: When overriding a method, ensure the input and output types remain compatible with the superclass’s method.
  2. Don’t override unnecessarily: Only override methods when modifying the subclass’s behaviour is necessary. Unnecessary overriding can make code harder to read and debug.
  3. Use super() wisely: The super() function is a powerful tool, but avoid confusing behaviour by calling the superclass’s method at unexpected times.

Conclusion

Method overriding is a powerful feature in Python’s OOP arsenal. Understanding and correctly implementing it allows you to write more flexible and easier-to-maintain code. However, with great power comes great responsibility — use these techniques judiciously to keep your code clean, understandable, and efficient.