Making Classes Sortable & Comparable In Python

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
how's it going guys in this video I'm going to be showing you how you can create comparisons for your custom classes in Python because by default when you create a class python has no information regarding what part of the class to use when you are comparing one instance to another instance for example if we want to compare this person named Bob with the age of 19 to this person named James with the age of 22 python doesn't really know how to do that we didn't provide any information in our class regarding how python should perform that comparison and before we start I'm just going to quickly explain the code that I have on the screen so the first block contains a class called person with an initializer that gives each instance a name and an age then I have my main function which we use to actually run the code and this contains a list of this person class and I just called this variable people and inside here we have the person of Bob with the the age of 19 James with the age of 22 Maria with the age of 27 and X makina Elon stopped doing this L with the age of 22 then what I want to do is sort this list of people and print the result now if we were to actually run this we're going to get this type error that the less than operator is not supported between instances of person and person and that's because we did not Define that so how can we provide python with the necessary information to actually compare these objects well let's go to our class and add that necessary functionality now before I do anything I do want to add the repper method because right now if we were to print this list of people we would get the representation of the class which is a memory address and that's not really useful for this example so here I want to change that up a bit by returning a string which will be this F string that contains self. Name colon and self. AG something that we can read so next time we actually print this class we're going going to get Bob and 19 instead of person class at the memory address but moving on if you want to make your class comparable you're going to have to define the LT Dunder method which also stands for less than so to do this we're going to type in Def LT and it takes self and other which is going to be a person class so I'm just going to import the type of self and this will return to us a Boolean so if the left side is less than the right side this will return true so here we get to define the necessary functionality for this comparison and I'm going to return the self. AG is less than the other. AG and that's how we will do the comparison we're going to compare the ages and that's how it's going to sort this list of people now the next time we actually run this script we're going to get it back sorted by the age so elon's child will be be first with the age of 12 because that is the lowest age then we will get Bob James and Maria so with that Dunder method we gave python all the necessary information that it needed to perform those comparisons and return to us the sorted list and we don't have to sort it like this we can also print people sorted and that will work exactly the same way and thanks to defining less than we can also just compare people one by one we can say people at the index of zero which is Bob and we can check that that is less than people at the index of one which is going to be James with the age of 22 so obviously that's going to return true because Bob is younger than James and what's cool about this is that it also works in the other sense if we were to run that it's going to return false because people at the index of one Bob is not older than James so that returns false so just by defining less than python was able to use this also for greater than but if you want some specific functionality for greater than I recommend you actually Define that by using GT which stands for greater than and it works exactly the same way it returns to us a Boolean when we compare it to something else and all we have to do is return self. AG is greater than other. Ag and what I like to do usually when I'm testing out new Dunder methods is actually give some sort of log message telling me when it's being used so there I'll write using LT and here I'll type in using GT so when we go back down to our print statement and we run the program it will tell us exactly what's going on so right now we are using greater than because the arrow is going to the right but as soon as we use less than we will get the less than Dunder method being used and finally if you really want to make them completely comparable you can also use the equals Thunder method so this of course will be self and will return to us a Boolean then all you need to return is self. AG is equal to other. AG if that's what you want to compare maybe you want to compare everything inside the class instead so that you can see that all the information is the same and that will work too you can also return that the name is equal to the other name and that's fine as well whatever you want to return in regards to comparing two objects to see that they are equal is up to you personally I would probably return a dictionary containing all the attributes or even if the class has an ID I would probably return the ID because if something has the exact same ID probably it's going to be the same thing so now we can also use the equals to operator and when we run that it will work just fine so that just about covers everything I wanted to discuss in today's video do let me know in the comment section down below if I missed anything or if you have any questions regarding this functionality otherwise I hope you enjoyed this video and with all that being said thanks for watching and I'll see you in the next video
Info
Channel: Indently
Views: 5,840
Rating: undefined out of 5
Keywords: pyton, pyhton, pythn
Id: znTV0nHmVD0
Channel Id: undefined
Length: 6min 9sec (369 seconds)
Published: Tue Dec 26 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.