Overloading Overwriting

Method Overriding vs Overloading in Python [+Video]

Method overloading: allowing different parameters for calling the same method

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

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:

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 callee 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:


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")

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 5,235 ambitious coders on my email list — and learn the Python vocabularity from scratch!

Leave a Comment