hey guys welcome to the next blog on python tutorial for beginners in this blog we will talk about operator overloading in Python now you might already know that everything is an object in Python so when I want to know the type of this number two it's going to show this kind of output and this means that two is an object of the class int now when I once again want to know the type of let's say 2.0 it's an object of class float when I want to do the same operation with the string this string is an object of class strings so everything is an object in Python now have you ever wondered that when you do two plus two it's going to give us the addition of two numbers and when I add two strings let's say two and then I am going to use the same operator which is plus operator with the other string it's going to give me the concatenation of these two string values so the same operator plus is working differently with different type of objects the same you might have observed with the multiplication operator which is this Asterix so when I use this Asterix or multiplication operator with a string and let's say I write three here it's going to print this given string three times but the same operator when I will use with the two numbers it's going to give me the different behavior which is the multiplication of these two numbers so the same operator Asterix is functioning differently with different kind of data or in other words we can say that this Asterix operator or this plus operator are overloaded for a different kind of objects so whenever this plus operator is used with two numbers it's going to give us the sum of these two numbers and whenever this operator is used with the string it's going to give us the concatenation of these two strings so both these operators are overloaded for handling different kind of data so this is the overloading of the built-in operators now we might also think that if this is possible can we overload the operators by our custom classes so let me create a class and I'm going to name it as the number class which is going to take one argument so let me define a constructor here so underscore underscore init underscore underscore and this is going to take one argument which is the number and I'm going to just write self dot num is equal to num so this is the simple class which we have created and now I'm going to create two instance of the same class so number and I'm going to give the number one let's say here and then I'm going to create a second object of this class and this time I'm going to give the number two here so is it possible that we can use let's say + 1 + + 2 so let's try this and it's going to give us this error which says unsupported operand type 4 + number and number so because Python doesn't know how to use this + operator with your class objects it's going to give us this error which is unsupported operand type and whatever is the operator you are using so it turns out that we can overload these kind of operators in Python by implementing some inbuilt methods in our class so we already know that whenever we want to implement a constructor in Python we use this init method so I'm going to create a very simple class and I'm going to name it as a and this will be a blank class so this is a very simple class in Python and when I use dir function which is a built-in function with this class let's see what happens so I'm going to just press ENTER and it's going to print some methods here which are inbuilt method so here not all methods are visible so I'm going to assign this value of dir a to a variable a and then we will see in the right-hand side in this inspector here and this is the a variable and you will see the list of all the inbuilt methods which are associated or which you can implement them so for example we have implemented the init method in order to have the functionality similar to constructor but there are other methods here too so there is a STR method or le method which is less than method or any method which is not equal to method and many other methods which we can implement here in our class so now I have given enough theory to you and let's try to see with an example how to overload an operator in Python so here I have created a class which I call circle and this takes one argument which is the radius of the circle and I have created the setter and getter method for this radius variable and in the third method I am just calculating the area of the circle using the radius and I have also used a inbuilt module which is matte in order to get the value of pi so using this expression I'm just calculating the area of the circle now I have created two objects of this class with two different radiuses so in the first object I have given the radius two and in the second object I have given the radius three and from the theory which we have learned till now we already know that this expression will not work with these two objects so when I run this code it's going to give me the error which says unsupported operand error so now we are going to overload this plus operator in this circle class now in order to overload a particular operator let's say plus you need to implement associated method in your class so with this plus operator there is a special method which we need to implement in our class and I have given the list of all the methods which is associated with the operators so with the plus operator you need to implement this special method which is underscore underscore add underscore underscore in order to overload - you use this method in order to overload the multiplication you use this method so here is the operator which we want to overload and here I have given the method name which you need to implement in your class in order to overload this operator in Python so there is this list which you can use with all these operators so you can overload the mathematical operators in Python you can also overload the bitwise operators in Python like and or or bitwise right shift orbit wise left shift and also you can overload the comparison operators in python using these associated methods with these operators so now we want to overload this plus operator so we will implement this add method into our class so let's implement this add method into our class and implementing this method is really simple you just need to define a special method which is underscore underscore add underscore underscore and this add method is going to take two arguments one is self you already know and the other is the object of the other instance so this second argument is the object of the circle class and now inside this method I am going to just return first of all I will just use the circle as the constructor and what I want to do I want to add let's say the radius of the two objects which are provided one is the self object and other is the object which is provided using this argument so I am going to just say self dot underscore underscore radius plus the circle object radius so let's use the circle object and then dot and then underscore underscore radius so basically I'm adding the radius of the two objects one is this circle object and other is the radius of the self which is the current class so as soon as you implement this method ad using these two arguments then this operation is going to work in Python so because we have overloaded the plus operator using this add method now this operation is going to work so let me just assign the addition of these two objects into a third variable and this variable I'm going to name it as c3 and using this c3 let's say I want to print the radius of the circle so I'm going to just use the print method and then I'm going to use the c3 object and I'm going to call our get radius method from this object and let's print the radius of c1 and c2 also so here I am going to just write c1 and here I'm going to just write c2 so we are printing the radius of all these three objects and I'm going to run the program now and you can see here the radius of the first object is 2 because we have provided 2 as an argument here the second is 3 because we have provided 3 as an argument here and because we have created this third object using the overloading of this plus operator where we were just adding the radius of both the objects that's why we got 5 years so 2 plus three is equal to five and this addition we have done using this expression so we have added the radius of the current class and the object which is passed using this argument so this is how you can overload the operators in python by implementing the Associated methods which are available in Python so let's say now we want to overload the less than and the greater than operator so for the less than operator we use this LT method and for the greater than operator we use this method called GT so let's implement these two method inside our classes so I'm going to just copy this add method two times and first of all I'm going to replace this by LT which is less than and the second method I'm going to replace by GT which is greater than and we want to see if the radius of the circle is greater than the second radius which is provided using this object or not so first is less than so we are going to provide this operator and second is greater than so we are going to provide the other operator which is greater than operator and now this less than and greater than function is going to give us the boolean value if this radius is less than this radius then it's going to return us true otherwise it's going to return us the false so I already have the two instance of two classes and if I want to compare these two classes I can just write c1 and I will use the less than operator here and then c2 and also I'm going to use the greater than operator here and let's run the code and let's see what happens so now it's going to give us this value which is the constructor of the circle so how can we get the boolean value you just need to remove this constructor from here so you just need to remove the name or the class and now I'm going to run the code once again and now you will see true and false so we were checking if c1 radius is less than C 2 and it gives us true and once again we were checking if c1 radius is greater than C 2 and it has given false to us also we can check the third object for example C 3 here if C 3 is greater than C 2 or not and most probably is going to give us true and it's correct here so you can see it's that simple to overload any operator in python using these built-in methods so you just need to find out the Associated method related to that operator and then you just need to implement that method related to that operator in addition you can also implement some other methods which are available with the class so once again when I'm going to print the dir with any of the object let's say C 1 and I'm going to run this program and you can see the list of the functions which you can implement inside your class so ad is already there but you can also implement this dish method or dir method or any of these methods which are available here so our EPR method or STR method any method you can implement inside your class in order to avail the functionality associated with that method so at last let's try to implement one of the method which are associated with the class so let's say we want to implement this method which is the STR method so I'm going to just minimize this and I'm going to just copy this function and once again paste here and instead of greater than I'm going to just implement now the STR method now this STR method only takes one argument which is self so I'm going to just remove the second argument and here in the return value I can just return some string for example so I can just say circle yeah and I'm going to return the area of the circle so I'm going to use this concatenation operator and then I'm going to convert the area into the string and then I'm going to just use self dot area which is going to give us the area of that circle so once you have implemented this STR method in your class you can simply use it like this dir method so I can just write STR and then the name of your object which you have created and let's run the code and let's see what happens so I'm going to just see the result now and it prints circle area and it's going to print the area of the circle I just need to provide equals to symbol here so we will be able to see it in a prettier way so you can see the area of the circle is printed similarly you can print the area of the other two circle which is circle two and circle three and let's run the code once again it's going to print us the area of all these circles so this is how you can use operator overloading in Python I hope you have learned something new this time and I will see you in the next blog you
Post a Comment