Method Overriding vs Overloading in Python [+Video]

Rate this post

Method overriding vs overloading in Python—what’s the difference? If you’re short on time—here it is:

  • Method overloading: creating a method that can be called with different arguments such as m() and m(1, 2, 3).
  • Method overriding: overwriting the functionality of a method defined in a parent class.

In method overloading, methods in a given class have the same name but different signatures (=argument lists). In method overriding, methods have the same name and same signatures, and child class methods override parent class methods.

The following video explains this article in more detail:

Python Overriding vs Overloading [Simply Explained]

Method Overloading

Method overloading: creating a method that can be called with different arguments.

Here is an example for Method overloading:

# Method overloading in Python

class Wizard:

    def __init__(self, mana):
        self.mana = mana

    def stupor(self, other, damage=50):
        if self.mana>100:
            other.mana -= damage

tom = Wizard(112)
dumbledore = Wizard(151)

# duell
dumbledore.stupor(tom, 200)
for i in range(10):

# Tom Mana:

# Dumbledore Mana:

Exercise: What’s the output of this code?

The class Wizard defines an instance attribute 'mana' that represents the energy level of the respective Wizard instance. If the mana level is drained, they cannot do magic tricks anymore.

The magic trick stupor reduces the mana level of an opponent (called other) if the called instance has enough mana left.

You can call the method with two alternative parameter settings. First, you can call it using only a reference to the other wizard instance. Second, you can call it by specifying both the reference to the other wizard AND the damage it performs.

In this way, you “overload” the definition of the method. It performs different things when called differently.

This is demonstrated in the output of the puzzle. Say, Wizard Tom fights against Wizard Dumbledore. Tom strikes first. He reduces the mana level by 50 points (the default “damage” when not defined explicitly). Dumbledore strikes back by reducing Tom’s mana level by 200 points in one hit. At this point, Tom has -88 mana left, so no matter how often he tries to damage Dumbledore — it doesn’t change a thing.

This is the result of the code puzzle:


Method Overriding

Method overriding: overwriting the functionality of a method defined in a parent class.

Here is an example for method overriding:

# Method overriding in Python

class Muggle:

    def convince(self, other, thing):
        print("Please, " + other + ", " + thing) 

class Wizard(Muggle):

    def convince(self, other, thing):
        print("Imperius {" + other + ", " + thing + "}")

petunia = Muggle()
harry = Wizard()

petunia.convince("Harry", "clean up")
harry.convince("Dudley", "clean up")

Exercise: What’s the output of this code puzzle?

We use a Harry Potter example. The class Muggle is the parent class of the class Wizard (indicated by the Wizard class definition Wizard(Muggle)).

Therefore, the Wizard class inherits all methods and attributes from the Muggle parent class — including the method convince.

However, a Wizard has other means to convince someone to do something. Thus, we OVERRIDE the convince() method in the child class. In essence, we replace the functionality with new functionality that is more specific to the child class.

When the Wizard Harry executes the method convince, it uses the method functionality defined in the Wizard class description.

Please, Harry, clean up
Imperius {Dudley, clean up}

Where to Go From Here?

You need to know the vocabulary of a natural language to communicate with other persons. Similarly, you need to know the programming language syntax before you can efficiently communicate with your computer, and other coders.

Join 55,235 ambitious coders on my email list — and learn the Python vocabulary from scratch!