How To Use Dunder Methods In Python Tutorial (Magic Methods)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
how's it going guys in this video we're going to be talking about Dunder methods and dander methods stand for double underscore methods that or it doesn't really stand for but that's what it means a double underscore method is a Dunder method and if you've ever built a class the first Dunder method you probably Learned was the init Dunder method the initializer as you can see it has two leading underscores and two trailing underscores that's why they're called Thunder methods otherwise you can also refer to them as magic methods both of those terms work when you are referring to these methods in Python anyway what do we use Dunder methods for well as a programmer we would use these methods to give functionality to our user defined objects so we can actually tell python essentially how to treat our class when we perform a certain operation for example you might have a class called fruit and first of all we want to add an initializer which returns none and maybe we will ask for a name so we'll say name of type string so self. name is going to equal that name so now we have a class called fruit that can take a name and we can also create an instance of this fruit we can say banana of type fruit is going to equal a fruit called banana so the first under method we use here takes care of instantiating our object we don't call this method anywhere in our code it automatically happens as soon as we tell python we want to create an instance it is called called under the hood and it's part of the functionality that comes with our fruit class but let's suppose we want to multiply fruit if we were to say okay banana Time 4 and we were to run that we would get unsupported operant types because it does not have that functionality but we can provide that functionality via a Dunder method we can go here and we can type in Def double underscore or thunder multiply and there are a lot of these so right here it takes self which is the current instance and the other which here I want to be of type integer and that's going to return to us I guess a string because we're just going to multiply that fruit many times so return self. name times other now we successfully defined the functionality that we want to use when we are multiplying our fruit by an integer so if we were to run this code again we would get banana banana banana because that's the functionality we defined in our Dunder method and again we did not call this directly it was called indirectly when we used the multiplication operator or the asterisk and I believe you can call these directly if you want you can also type in thundercore multiply and you can add four and that will work as well that's just not that common and looks incredibly ugly and we want clean code because the python Zen forces us to be its slave so we will just do things conven Ally and there are many Dunder methods there are so many Dunder methods that you can play around with if you want to explore them you can just add def underscore and your code editor will give you a lot of suggestions another thing you can do is tell the program what to do if you want to convert it to an integer so here we're going to say okay if we use the int method on our fruit this is going to happen and this should return an integer so we're going to return the length of the fruit that's what we're going to use as our custom functionality I'm not saying you should do this in a real code base this is just to demonstrate that you can use the int method now as soon as you have defined it with your custom class so here we'll type in self. name and the next time we take the length of banana or not the length but the next time we try to convert banana to an integer you'll see we're going to get the integer of banana back which is six letters so we're going to get six back now of course as I mentioned earlier this is probably probably a stupid idea because it's not that intuitive so what I should have done here is defined the length Thunder method this is much more fitting than the integer for this case because we're taking the length of the name and we're returning it the next time we decide to use the length of the banana which of course will return six but you can do something else you can also always return zero if you don't want it to return the length of the string you can return whatever you want and that's all we're doing with Dunder methods we're providing that functionality that you would get with a lot of the inbuilt types to our custom classes so if you are new to python I absolutely recommend you play around with these because there are so many you can use if you just type in Def Dore again you will get a lot of suggestions for Dunder methods that you can use with your class another one that's very common is the string representation so if you were to type in string this would return a string and here we would return self. name the name of the banana so so the next time you actually print this Banana by itself it's not going to give you some crazy some crazy object with a memory address it's going to give you the actual banana name if we were to remove this line of code here you would get the representation which is illegible to a normal human of course we know what it is because we created this fruit but if you want it to be readable you would have to add a string Dunder method or you would have to change the representation which is the default for classes so if you were to change this also to the same thing you will also get banana back but for the representation it's usually a good idea to return something that's useful for the developer so the default which is this fruit object makes sense for a representation but if you define a string method of course it's nice to see the banana name in the console anyway I really hope this video helped you to understand what a Dunder method is of course there's much more you can do with thunder methods than what I showed you there are so many you can use and I think they're super cool because of course being able to Define this custom functionality with your class is always cool but I really must stress it's good if you follow conventions and you make it as straight forward as possible don't make your string method return an integer for example try to follow as much logic as possible but anyway I would love to hear what you think about all of this in the comment section down below whether you have some tips and tricks you would like to share with the rest of of us and with all that being said as always thanks for watching and I'll see you in the next video
Info
Channel: Indently
Views: 14,457
Rating: undefined out of 5
Keywords: pyton, pyhton, pythn
Id: 1I3fuDR2S9A
Channel Id: undefined
Length: 6min 36sec (396 seconds)
Published: Wed Dec 13 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.