The code below shows that when working with different object types like, int, str or list, the + operator performs different functions. This is known as operator overloading and is another form of polymorphism.

# For an int and an int, + returns an int 2 + 4 == 6 # For a string and a string, + returns a string "Is this " + "addition?" == "Is this addition?" # For a list and a list, + returns a list [1, 2] + [3, 4] == [1, 2, 3, 4]

To implement this behavior, we must first discuss dunder methods. Every defined class in Python has access to a group of these special methods. We’ve explored a few already, the constructor __init__() and the string representation method __repr__(). The name dunder method is derived from the Double UNDERscores that surround the name of each method.

Recall that the __repr__() method takes only one parameter, self, and must return a string value. The returned value should be a string representation of the class, which can be seen by using print() on an instance of the class. Review the sample code below for an example of how this method is used.

Defining a class’s dunder methods is a way to perform operator overloading.

class Animal: def __init__(self, name): self.name = name def __repr__(self): return self.name def __add__(self, another_animal): return Animal(self.name + another_animal.name) a1 = Animal("Horse") a2 = Animal("Penguin") a3 = a1 + a2 print(a1) # Prints "Horse" print(a2) # Prints "Penguin" print(a3) # Prints "HorsePenguin"

The above code has the class Animal with a dunder method, .__add__(). This defines the + operator behavior when used on objects of this class type. The method returns a new Animal object with the names of the operand objects concatenated. In this example, we have created a "HorsePenguin"!

The line of code a3 = a1 + a2 invokes the .__add__() method of the left operand, a1, with the right operand a2 passed as an argument. The name attributes of a1 and a2 are concatenated using the .__add__() parameters, self and another_animal. The resulting string is used as the name of a new Animal object which is returned to become the value of a3.



There is now a Meeting class with an attendees list attribute and an .__add__() dunder method that adds Employee instances to the attendees list. Before we try and add employees to a meeting, we want to make sure we can know how many employees are in a meeting.

Inside the Meeting class:

  • Overload the len() operation by defining a __len__() dunder method
  • Inside the __len__() definition, return the length of the attribute attendees

Now add three employees to a meeting:

  • Using the Meeting instance m1, add each of the employee instances e1, e2, and e3. Use one line for each employee instance.
  • Output the length of meeting instance m1

You should see the output from each employee being added and then the length of the meeting, 3.

Sign up to start coding

Mini Info Outline Icon
By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.

Or sign up using:

Already have an account?