Py #2 What is Operator Overloading?

Göker Güner
Python Blog
Published in
3 min readApr 5, 2021

--

Hello, in the second article of the series where we talk about the useful and unknown features of the Python language, we will talk about the concept of Operator Overloading, which can be quite practical.

If you are not interested in the Turkish of this series, you can skip this paragraph.

Merhabalar, Python dilinin kullanışlı ve pek bilinmeyen özelliklerine değindiğimiz serinin ikinci yazısında oldukça pratik kullanım alanları olabilen Operator Overloading kavramından bahsedeceğiz.

Yazının Türkçesi için: link

Python is a software language that you can do almost anything you want.

Python is a software language that you can do almost anything you want, as it has a large community and a rich variety of libraries. However, apart from these varieties, the language has its own useful features. Operator Overloading is one of these features.

We can briefly define this feature as adding new features to the features of operators.

First, let’s take a closer look at the methods and properties of the int data type using the dir() function, one of Python’s built-in methods.

In this article, we will use __add__ and __repr__ methods as examples for our topic. To see what the methods do, you can use the help() function as follows.

This method does the addition we know.

I wrote this as an example of the use of the method. As anyone familiar with the Python language at the beginner level knows, I could also use this method by typing “3 + 5”.

The __repr__ method also determines how the print() function works, that is, how the variable it receives will be displayed.

These methods belong to the int data type, so when we want to overload, we need to do it in a class. When we want to do it without belonging anywhere;

We see that it does not have any effect. This is what the self parameter these methods take to tell us, it must belong to a class in order to be manipulated. Now let’s move on to our sample work.

We create a class that we call Order. This class will be created simply by getting an order list. Thanks to the *args variable we provide when initializing our class, our list can be as long as we want.

Next, we define the methods we want to overload and determine how to do this. For example, when we use the add method, the new element we want to add will “append” to our first list. Normally when we want to do this;

We receive an error that only one list can be added to a list. However, we give it this capability by overloading the addition process in our class.

Another overloading operation is related to our __repr__ method. Again, we add the variable in the string type we define with the name of the header to the method we define in our class. Thus, when we want to print a variable belonging to this class, namely an order list, it will print it by putting the “Order List: ” heading in front of it.

When we want to print our list as a normal Python list;

We get a printout in the form of. Next is to see the outputs of our overloading operations. Let’s start by defining our variable belonging to the class that we have defined.

Let’s try printing the current version of our list on the screen to see if our initial overloading worked.

Now, let’s try to repeat the operation that we got the error when we just wanted to;

Thus, we changed the two methods we chose within the class we defined, giving them new abilities. See you in our next work.

--

--

Göker Güner
Python Blog

YTU Alumni. ML Ops & Engineer at AlternaCX. Data&AI Enthusiast. MS Student at Bahcesehir University AI Program with Thesis.