Get startedGet started for free

Overloading Python Operators

1. Overloading Python Operators

Nice work! Let's take a closer look at operator overloading.

2. Overloading comparison operators

In the last lesson, we saw how the __eq__ method could be used to customize how the equality operator works for objects of the class Person. __eq__ takes the parameters self and other, which allows for two objects to be compared. Here, we use the name attributes as a proxy for determining if two Person object are equal. __eq__ should return a boolean value; True if the objects are equal, and False otherwise Let's try doing something similar for other Python operators.

3. Adding two objects

The Team class shown here stores a list of names using the team_members instance attribute. Once Team has been defined, we create two Team objects; rookies, and veterans. We'd like to create a dream team by combining rookies and veterans with the plus operator. However, since we tried to use the + operator with Team objects before it had been overloaded, we get the error below. Let's fix this!

4. Overloading the + operator

Overloading the plus operator is just like overloading the equality operator. This time, we'll use the magic method __add__. Just like __eq__, __add__ takes two parameters; self and other. When two Team objects are added, we'd like to create a larger team. There are a few ways that we can do this. Since the team_members attribute of both self and other are lists, we can use plus to add them and create a list containing all team_members. Then, this list can be used to create a new Team object, which is then returned. Let's take a look at this in action!

5. Adding two objects

Like before, we'll define two Team objects; rookies, and veterans. Now that we've overloaded the plus operator, we can add these two teams together to create the dream team. dream_team is an object of type Team, and has four team_members; two from rookies, and two for veterans. If we wanted to create another team using the dream_team, we could again use the plus operator.

6. Using + to create a new type of object

Alright, we've used plus to create a larger Team from two smaller Teams. But what if we to create a team using Employees? Let's take a look. To do this, we'll overload the __add__ magic method in the Employee class. Like before, __add__ will take the self and other parameters. This time, we create a two-element list using the name attribute of self and other. This list is then used to create a Team object, which is finally returned. In just a few lines of code, we've customized the plus operator to create a Team object when two Employee objects are added. Check it out!

7. Add two objects to create a new object

First, we'll create two employees, Anna and Jeff, using their name and titles. To create an audio team, we can use the plus operator to add Anna and Jeff. The result is stored in the variable audio_team. When we inspect audio_team, we see that it's an object of type Team, and has two team members, Anna and Jeffrey.

8. Overloading other operators

In addition to the == and + operators, there are dozens of others that can be overloaded. Here are just a few common ones!

9. Let's practice!

See, overloading operators can be pretty powerful. Time to give this a try for yourself with a few exercises.