Python's magic methods

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
I know it's been a little while since I recorded a video sorry about that I do have my reasons how I will um I'll talk about those in either a separate video or in a community post or something I don't want to saturate this video but I am back now and I'm going to be doing probably a video a week going forwards again I'll have more details about that in the uh in the little update I'll do but today we are looking into Python's magic methods so these are kind of special uh little methods they are they can actually be called special methods they can also be called Thunder methods and these have various purposes within a class so they can be you know a Constructor they can you know control how the class is represented they can control operator overloads for example so yeah these things are very useful and there are a lot of them in this video I'm not going to go over all of them I'm going to go over a decent chunk of them it helps a lot of them are quite closely related so I can actually go over quite a lot of them without having to talk about a great deal of them um so this video won't be too long but there are plenty more for you to kind of scavenge around if you want to find out but these are you know if you're especially if you're new to object oriented programming or if you're just looking to get you know a little bit better with classes and looking to you know answer some questions about how to represent certain things or how to do certain operations uh then I'm here to show you that and I'm just going to add one to my notes because I just remembered there's one I particularly want to talk about so we'll start by creating class and we're going to do kind of my you know my go-to and we're going to create a human class and this class is going to have the name the age and the jobs uh I'm going to set that as none by default of the person and then we're going to have self.name equals name that would help if I actually did that properly self.age equals age self.jobs equals jobs nice simple thing this one uses the init uh special or magic method whatever you want to call it and this is probably the one that you would have seen the most because if you've created any class uh in Python then you probably would have used this this is the main Constructor this initializes the class hence why it's called init um and you don't have to provide it though generally speaking at about 90 of instances you probably will want to do something with it unless you're using like data classes or etchers or something then you don't have to worry about it but I'm not going to spend too long talking about that but I do want to talk about one called death in it uh subclass I'm not going to go into huge details about this um but I do yeah I did want to bring it up uh we can get rid of the tie pins we don't need to complicate matters and if we just say you know Meme equals none here uh and I'll show you how this works so if you just set a class attribute here and then to say we had a subclass called like test or something and it inherited from human and we don't have to do any of that but when we initialize a subclass this actually gets called and this kind of you know takes over the role of meta classes in some respects I'm not going to talk about meta classes today um but this this class is you know the test class and in the meme here what we can actually do is we can pass it in here so say if we do like Rickroll for example I'm not going to bother with this class attributes actually I feel as though it's just going to complicate things but we can print Meme and then if we run that whoops I've got one called Magic plan which is why that worked we get Rick Roll so essentially what's going on is we're creating this we're inheriting from Human this in it some classes being run it's taking this which is an instance not an instance it's the class of test and then anything we pass in here as you know arguments all keyword arguments then get past this in its subclass so this is useful if you want to like register subclasses within the class or that's to be honest that's the only use case I can think of I did look this up I haven't really found a use for this ever um but you know there may be some people that do have a use for this sort of thing so I thought I'd bring it up while I was talking about the magic methods I'm not going to talk about it in any more detail than that this might be a separate video on its own but I thought I'd mention it considering I thought it was cool so we're going to move on to kind of representing classes in different ways um so you know how to represent it is a string is a particularly common one you can use this def string and then we can say return self.name so now if we were to you know print um our human object say if we you know create one for me my name's Ethan I'm 24 years old then when we print the actual class itself then we print what's in the self.name if we didn't do that then we'd end up with this here so this provides a human readable um way of you know using it in a string if you want a more detailed version of that you can also do wrapper and this is like the string representation of the class so in here you'd potentially want to do something like you're human this is kind of the data class format um so you could do yourself.name and then age you know because self-directed you're not going to bother putting jobs in there but now if you print that it still does the string representation by default if string isn't defined it will do the wrapper by default if you can also use wrapper here and then it would print the string represent or the detail the string representation I guess you call it and this is really useful for like debugging and stuff like that um you know it allows you to see you know kind of that you're manipulating the right thing and one particularly common traits with these Thunder methods is that they tend to Define uh what happens when the object is called in certain circumstances so for example the wrapper function the dunder wrapper is called if you were to do like an INT for example then you would use def uh dunda int not to be confused with dunda in it they are you know very similar in this case we can do soft age and now a print 24 to the console you can also have float which returns a float number and you can also have hash uh I'll just do hash real quick where you essentially just do like your own custom hash I don't remember I think if you don't Define this it will just do it maybe let's let's find out let's find out live I didn't test this before there we go it automatically does one for you so you don't need to Define it but you can Define your own one if you want so you know if you want say two classes with the same name to have the same hash and you can do that or you can provide a hash that was a tuple of all of them I think that might be what it does by default um but it's worth noting nonetheless so with representations out the way that's probably not all of them you know there's there's complex and stuff as well but you know I'm just skimming over the ones that are potentially more useful um you know you can always look up if you have any more queries but I'm going to move on to operator overloads now so I'm only really going to talk about one of these in detail because a lot of them work kind of the same um so I'll leave all the rest there so if you want to override what happens when you add two classes together for example you can use the dunder ad so this would be you know say if you wanted to do um well say he had me which is a human the name Ethan and I'm 24 and he had Barney the dinosaur I don't know I don't actually know how long but it is the dinosaur has been on TV but let's say 50 years I don't know if that's right that's probably a bit much um and then we wanted to do you know add the two together like this so we uh so we want to like control what happens when we use the plus operator what we can do is we can have this South and this other is kind of the other class so the self is this end or this side and the other is the right hand side so Ethan of yourself and Barney would be other in this instance and then we can you know do something like return self.age plus other diets and if we did that then we get 74 because 24 plus 50 is 74. one thing to keep in mind is that you might not want to you know do this um if the two objects are of different types so say for example you know Ethan was a human and then Barney you know we created another dinosaur class that maybe didn't have age and then it would error and we didn't you know want to deal with that what we can do is we can actually do the check and if you work with my part it will actually tell you to do this you could do um if not is instance uh other and then we can do self.class whoops I don't know how that happened we could do is we can return not implemented and it's important that we don't raise not implemented error here because some of these done the method and this is only a thing you do in Thunder methods uh some of these done the methods kind of have alternative implementations that they then do if not implemented is returned others I think this one it would just raise an error immediately but in other methods is recommended that you do this but essentially what this does uh if the you know the other if the right hand side operation is not the class you expect it to be then you can return this and then python would handle it uh from there so I think maybe in one of the list based ones or like the sort perhaps it does something with it I'm not really sure I haven't looked too much into that so on top of ads you also have sub for subtraction mole for multiplication floor div and true div so floor div is if you were to do say um with a double slash and true div is a single slash uh you know have mods which is the modular operator like that and then you have pal which is the double asterisk operator like that and you can override all of these uh I'm not going to go over every single one because the things would be largely the same you know the the only thing that we're changing the subtraction one is I'd do that you know um yeah these would be something to implement on your own so on top of all these operators you also have additional operators that can be used with plus equal so for example we want to do Ethan plus equals Barney and then print Ethan the ads um uh done the method wouldn't be sufficient for that we would need to do the def I add and then that takes us off another and this is largely the same you know you'd have uh these same things instead of doing this you do self.age plus equals other.h and then you would actually return self uh so it's a little different but now if you run that then we actually print a correct thing or at least something is useful we can see the you know the age of me I've suddenly gotten 50 years older while recording this video um so yeah this I you know you have your I add I sub I'm all I floor div I2 Divi mod ipower you have them for all of them and you know as I say the operation is going to be largely the same you just need to make sure that you return self uh because you know obviously what you return over here is what's going to be set is what Ethan is going to be set to um so do keep that in mind next I'm going to talk about comparison operators uh so you know if you do something like this or something like this we can also put that back in a print statement now so with an equals for example you would want to do a def EQ self the other and you would have this same thing let's check here but one thing you could do instead if you wanted to is just return false because if the two instances aren't of the same class then of course they're not going to be equal so you don't really need to return not permitted I believe you can and it would return false anyway but don't quote me on that I tend to just return false directly out of this and then you know in our case we could do you know self.name equals other dot name if you wanted to you know include the age as well so if you wanted to make sure that the class the equal people have the same name and the same age that you could do that or if you could do the jobs if you want in this case you know if you know two people have the same name then they're the same person because that's how it works in the real world I think as we all know you do also have uh def any as well um which is you know your uh not equals so that kind of works the same you can basically just program that as return not self.eq if you want like that and that'll work just fine you also have um Ellie as well which is less than or equal to LT which is just less than GE which is greater than or equal to and GT which is greater than now if I just kind of show one of them off so say LT again I want to be here for absolutely ever we could do self.age is less than other dot age so in our case less than it'd be the left hand operation is less than the right hand operation so do keep that in mind so if you just print that we can see that you know I am younger than Barney therefore the LT is true if I do this our names are not the same so the check comes out as false if you want to do any of these operations so if you want to do any of these comparison operators or if you want to do any of the operator overloads a lot of these things don't have inbuilt ones so the string and the wrapper do have inbuilt ones for example but you know operator overloads um and eqs and stuff don't unless you're like inheriting from another class or something to say if you are you know inheriting from the int base cost and it would have you know add I add EQ probably it would have as well all this stuff but on their own like a class on their own doesn't have all of this so if you do need it then you do need to add it yourself of course you don't need to add all of them but that's just something to keep in mind for those so while we're on the topic of operators I want to talk to you about something I actually learned the other day which kind of gave me the inspiration to do this video in the first place and that was def contains uh so we could do self and then key or it's supposed to be value maybe would be a bit better um so this is if we were to say you know if to do you know Ethan in like a list of some description it would then call this so This is actually wrong it would be if um say you know programmer in Ethan for example so if my jobs if I send my jobs real quick uh to programmer and then if I did you know return value in self.jobs then if you know programmer is in ethan.jobs which it is it will return once I get it right true see that's something I learned really recently when I was doing some like a class that handled version constraints um and that's a really nice little shortcut for something like that you know if it's like a dictionary subclass or a list subclass or anything like that now this video is getting a little bit long but there are there is one more thing that I want to talk about and that is the def gets item and to a slightly lesser extent get atra but we'll talk about that uh so so this is actually kind of works uh sort of the same way so if I were to do this in this example looking at the jobs and then key what I could do is I could do say prints Ethan and then you know slice it like a list and then it would be p interesting oh yeah because this is actually supposed to be a list isn't it that was kind of how that was designed to be there we go so now I don't have to you know Supply ethernet jobs what this is doing is it's calling get item the zero is going in here so this works on lists and digs so if Jaws was a dictionary for example I could do you know programmer here and you know a programmer could have a value as soon as we're doing a list it'll be a number um and then it would go in here it would then you know index the soft.jobs and then it returns out this is how numpy does its fancy syntax with its commas and commerce with its well yeah it does have commas in it actually and it's colons and all that I was going for columns and rows but means the same thing I suppose yeah there's also you know uh getatra which is only really useful if you have like um a config thing or like a dictionary so one example of this this is kind of something I did in the real world let me Zoom this in a bit it's like a configuration um class which kind of just holds configurations so it loads a config file and it does all this stuff and then you have this you know get atra which resolves a key from the environment variables and you have get item which does the same thing so this allows you to say you know ethan.p programmer for example if programmer wasn't something that was set up here you know maybe you could look it up in a dictionary um or something like that so yeah that's basically all the ones that I wanted to talk about there are so many others out there um and a lot of them are quite useful but this video is already getting pretty long it's longer than I thought it was going to be I'm glad I kind of limited it to what I did um but yeah if you found it helpful at any point then do consider leaving a like to let me know and maybe subscribing if you want to see more videos like this does anything particularly you want me to cover and make sure to leave a comment I do read them all so the feedback would be greatly appreciated if you want to support the channel monetarily you can do this on one of two ways the first by becoming a member by using Joy button below the second one become a patron by using a link in the description and one panel either and you can be on this screen like these people and I will see you in the next video where I will finish a video that I've kind of been working on before everything became really busy and I didn't have time so I did this one instead but the video should be really good if you're looking you know hardcore into the python 3.12 changes specifically the multi-parallelism stuff that video would be really good so make sure to watch that so I will see you then I guess
Info
Channel: Carberra
Views: 12,063
Rating: undefined out of 5
Keywords: pyfhon, pytho, pytbon, pytjon, ptyhon, pytyon, ptthon, pyyhon, pythn, pythoh, pythpn, ython, pytgon, pyhon, pytohn, phthon, oython, pthon, pyghon, pythoj, pythno, pythkn, ypthon, pytuon, lython, pyrhon, pythom, pythob, puthon, pgthon, python, pyhton, pythln, pythin, pytnon, pyton
Id: 7wn4x93gJcg
Channel Id: undefined
Length: 19min 30sec (1170 seconds)
Published: Mon Dec 05 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.