#59 Python Tutorial for Beginners | Operator Overloading | Polymorphism

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] my name is Ivan 20 I'll never give the city's on Python we have started talking about polymorphism right and in that we have seen one example of duck typing now in this video we'll talk about operator overloading so what is operator overloading so we know the concept of operators right so we have to offer example if you want to add two numbers you can say 5 plus 6 well 5 & 6 are the operands and plus is the operator so we know that right and then we have a concept of polymorphism so it simply means you have one thing which has multiple forms right now if you can think about this we have two integers and then we are trying to add them we can also add an integer and a float right so in different programming language it does support what about two strings so if you say you have two strings let's say hello plus world will it work and there's this yes right so plus works with the strings as well but what if you want to say you have a number five so let's say we got a and a is five right and then we got B and B is let's say void now can I print by saying a plus B the moment you try to on this code you can see we caught an error it says unsupported offering types 4 plus so we cannot use int and string 4 plus right so all these things are pretty fine all these things they are called as a synthetic sugar which simply means it is trying to simplify the code for the user sit behind the same things a bit different imagine this one example if I say a is 5 and B is 6 now what do you think what is happening behind the scene now trust me whatever happens in Python happens with the help of object right and here as well when I talk about a and B the type of it is this ink right so ink is a class here and when you say class of course class will have certain methods right so behind the scene what is happening is when you say a plus B which is of type integer it is calling something so behind this say it is calling int dot the moment you say int dot int is a plus right then you can see we have a method call in it we have ethical as abs and we also know mr. Collis ad which is very important here so I'm going to say ain't dot add this is taking two parameters okay the first one is a comma B so what we are doing here in print a plus B the same thing can be done here the question on this code here you can see be quite the same outputs of both our printing 11 so you can say a plus B or you can say ink dot add by passing two parameters a comma B and of course the first one looks cool right you're simply saying a plus B because from our childhood we are doing that we are trying to add numbers using plus operator but the moment you come to programming in programming whatever you want to do you will be doing that with the help of methods and another method which belongs to the int class right if I jump to integer so you can press down the control button and click on the method which you want to see on the class which you want to see you can see it's a class and if this class has multiple methods in the same way the moment you say ain't dot you can access a method call this ad and you're putting a comma B so even if you say a plus B behind the scene this is getting pulled right so this is one thing it remember okay so we'll talk about operator overloading later but this is something you have to remember so whenever you add two numbers this is what is getting called behind the scene now if these two things are string so if I make it string here now this is not integers right there string so I have to say STR dot add now when you say steer that and in fact STL also has add method which takes two parameters and both should be of the same type in a string type and then it will work right so if I learn this code and you can see it is working so we got five and six of the string so they call concatenate adhere so this works the moment you change the type of it will not work because the inbuilt class doesn't have two things which is integer and string together right so this thing it remember now once we know that moment you add a plus operator it calls the add method the moment you put a minus operator it will call a sub method the moment is use a star simple which is multiplication it will call one method so we have different methods for different operators right and not only those things are called as magic methods but that's what they say magic methods right so all these operators behind the scene they work as methods not to understand the concept of operator overloading what I will do is I will say class and let's say we have a student class and every student will have let's say two variables and that will be marks one and marks two just to keep it simple so what I will do here is I will say def and in it and here I will say self dot M one is equal to PO now we want the value from the user right so or maybe I want to pass the value so I will pass it from here M one comma and two and let's specify self dot and two is equal to M 2 so we got these two values right now what I want to do is I want to create two objects okay tutions object or one is let's say s 1 is equal to student and this will take two values I would say 58 comma 69 that's the value here and then s 2 is equal to a student and this student will have let's say 60 comma 65 so we called two marks here right regard to student and II shouldn't have two marks now I want to apply the operator here which is plus operator so I want to add this to students so the moment I say s1 plus s2 I want a different student object right is it possible to do that let's try so I would say s 3 is equal to s1 plus s2 because now we know that plus operator means it will add two values but it is possible with help of integer it is possible with help of string is it possible to use plus operator which student class that's a question so that's one this for let's see what happens okay we are not printing the value of s3 but let's run this code the moment you try to run this code you will get an error oh okay we got different error here it says student takes no argument but it is taking arguments right what's wrong oh we have written the wrong method name my bad let's run this code once again so I was expecting this error not the first one and the other is it says unsupported operating type plus we cannot use plus between student and student because we have not defined it right if you remember behind the scene even if you use plus with integers it will be calling a add method but if you see our class we don't have that add method right because if you say s1 plus s2 how your compiler how your Python will know what to do and that's where you have to define it yeah that's we have to say hey the moment anyone says plus of a student you need to call this method call has ADD okay so you can overload the operator and you can change the finishing for it you can you find anything you want right so I can say add and this and we'll take two parameters the first one is self comma other and you can see week takes two parameters itself and other okay guess what is happening so behind the scene this code is getting converted into it is student dot add which takes two parameters s 1 comma s 2 so what is self and what is added here so the first parameter is self and s 2 is the other parameter you can change the variable name that's fine okay you can say this is oh and it should work it's a variable name right so this is what is getting called behind the scene right so here what I will do is I will say let's take two variables M 1 M 1 is equal to self dot M 1 and M 2 is equal to self dot M 2 so we got two variables right oh not just self M 1 plus it is other dot M 1 plus other dot M 2 so this is how you add values right so self M 1 and other M 1 and then M 2 will have the addition of self and 2 and other n 2 so this works and once you've got these two values what you'll be doing is you will be creating a new student object will call the history you can have any name user matter so s 3 is equal to student by passing these two values M 1 comma M 2 and now once you've got this student you will return as 3 here right that is what you're expecting so the moment you say s 1 plus s 2 it will return the value it will return a new object of student and you'll assign that to s 3 ok so this is what will happen now if I print the value for x 3 so I will let me friend s 3 dot M 1 and the moment we have this code 1 and you can see it works we've got 1 1 8 is it right of course it should be right so it is 58 plus 60 and that's right we got 1 1 8 this thing is working right perfect so if you want to add two students you need to overload the operator of plus because integer knows when it's plus string knows what is plus your student class don't know what is that plus means so plus means call the add method but we don't have an ADD method here so we have to define our own method the same thing can be done with subtraction is - the same thing can be done with multiplication so I would recommend you to explore more on this you know so in the moment you say def underscore underscore you can see we have so many methods we also have greater than we also equal to symbol we can compare two objects we can say the different methods available you okay so try it out we also have multiplication somewhere so you can see if your thermal so everything is predefined you can just use them in fact for this example I will take one mode I want to compare if two objects are greater than or equal to example if I say if s 1 is greater than s 2 this is the multiple way of comparing students because marks is not a criteria where you can compassionate but just for the example I will say s 1 win so whoever has maximum marks I will say they've been I would say else French s to wins so what they're trying to do here is whoever has maximum marks they will win but when you say my exam marks how do you check it because we are not defining it right maybe I want to check one of the first marks which is M one or maybe I want to check and two or maybe the addition of both you could try it out so what I'm going to do here is this will work of course not because the moment you try to on this quote it will give you an error by saying the greater than symbol not supported between two instances of student but it's supported for integers because this pretty fine so here as well if you want to make it work you have to define a function of method which is GT GT is greater than GE is greater than equal to so you want to check when the greater than and then here you will be using self and other as usual because behind this thing that is what is happening it is saying student dot GT and it's passing two variables s1 s2 so it's one goes to self as two goes to other and now how do I compare since the s-1 doesn't matter is it as one of something so x1 is equal to self dot M 1 plus self dot M 2 and we'll say S 2 is equal to other dot M 1 plus other dot M 2 so we are adding their marks basically you can compare if s 1 is greater than s 2 now this time s 1 s 2 do not objects ok they are simple variables of integer ok if I get in confused with this let me just make it odd that should make sense so let's compare R 1 and R 2 now those are just marks right if R 1 is greater than R 2 we will say return true else return false simple now I feel this code we are comparing it right let's compare and let's run and it worked you can see s 1 beans right because swen values are higher so 58 plus 69 is higher than 60 plus 65 but what if I might change the value for s2 let's it is 69 and you can see of course now s2 is bigger so let's run this code and you can see it's a that's two wins so that's right so what you are doing is we are adding those values and then we are comparing it so if you want to perform any operation on the objects which I usually find you have to define all these methods okay there's one more what if you have a video but let's say a is equal to nine let me to say print a and you can see it will print the value of a there's nothing wrong with that you can see it is printing the value of a no is it printing the address of it that's not the case right is not printing the address of eight it is printing the value of a but what happens the moment you try to print s1 it will not bring the values of s 1 it will try to print the address of s1 okay so you can see that it says it's a student object at the address at this position hey we don't want address right we want values what is happening so when you try to print the object doesn't matter is it integer or your class behind the scene it is calling a method called as STR it is happening behind the scene okay even if you don't call it it is happening behind the scenes the moment you say print a it will try to call a dot STR and that's why you are getting the output right you are getting nine because it is calling STR in the same way now I'm going to say s1 even this is calling SD r-right and now I feel on this code you can see two still giving the same output that means it is calling STR what is this STR so if I click on this you can see this inbuilt end so even if you don't define STR function or method in your own class it is getting defined somewhere and that definition what is printing it is printing the module name it is printing the name of the class and it is printing the object address we don't want that we want values right that means we need to override this method so what I will do is I will say def and the method name is STR so you have to define this by yourself so we want to return the value of M 1 and n 2 right so you will say self dot M 1 comma self dot and 2 and now if I run this what it will be drawn a couple of course let's run this code and you call it can you live we got 58 69 so now if you want to print the object it will not print the address it will print the values right because we are overriding the STR so if you remove STR they fit on this quad you've got then either so it is returning a non string value have to return a string value how do we do that because by default when you say print you want to print a string right so what you will do is you will use a format here so you can say curly brackets curly brackets you're printing to stuff dot format and inside this format you can pass this to value so what will happen is those curly packets will be replaced by these values so you are returning a string now and we have seen that before right so let's run this code and it worked you can see quad 58 and 69 so even if you say print s1 it does work you can also print as - if you want and let you run this code and you can see we call it external 65 so that's perfect so this is how you work with operator overloading okay so point remember is whenever you perform any operator like addition subtraction deletion behind-the-scene we are calling methods okay so forth plus we use add method for - we use sub method and for star views much method in fact we have list of methods there right so let's stop this video I hope you enjoyed it in the comment section and do subscribe for further videos
Info
Channel: Telusko
Views: 205,024
Rating: 4.8913236 out of 5
Keywords: telusko, navin, reddy, tutorial, java, python
Id: 9wd50TKv_OQ
Channel Id: undefined
Length: 14min 29sec (869 seconds)
Published: Thu Sep 06 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.