How to use the specia method/function def __str__(self): return #code #python #method #programming
Transcript
Hello guys, welcome to another video. In this video we're going to look at this special method in Python. In that is attached normally to classes and normally or P programming paradigm. So we have a. This is the syntax of the method that you're talking about. South. I'm just going to create a simple version of the class. And this class is going to be. This class is going to be. Just like we did. So we're going to say class, then let's just say person. And then from this you're going to say. Define and ohh whether initialize. The method that's going to contain now the properties, so I'll just keep it simple. So the name. Umm, yeah, just name, OK. So this is what you have done before guys, so. Photo is that maybe, maybe you're wondering what is happening. So this is a say a class, this is a keyword and then this is the actual name of the class that we want to. We are thinking of that as a world object. And then this is a function simply called a method. OK, so this is called init or whether stand for initialization. So we are now initializing. What properties this is going to have? So just a name property? OK. On and and normally that is like. Variables that you have. So now what's going to happen is we're going to say serve dot. Now we assign a variable. I mean you assign into value. So save the name equals to just name. So the best way we can do this is this. On your left you have a valuable and then on the right you have the value of that variable. Which is in this which is just the property. So now. What you're going to do now is we are going to create this spatial method. Now this special method is what actually helps us to understand what object, what is, what was passed into the object. That is attached to this class Payson. So from there what you're saying is create this special method. So this is what a function called simple method. So this is going to under score under score is the hour under score under score and we are going to just requires one argument and that is over the parameter that is essay itself. So the reason why we are putting the self is because whatever property is being initialized or attached to the collar spacing will also be passed on to this. OK, so this is more like an indirect inheritance. So from here we just say we turn. OK, so I'm going to format this string. I'm going to say. Umm. Maybe this return? Whatever the name. So self dot name. OK. Then once that is done, now you can go on to create now the object to map the class into objects. So the different people or individuals or pacing who have different names so you can have as many objects as you want. So I'm going to say T1 or person number one is equal to whatever the class we are dealing with. And then now you can see something that pops up on 2 just on top here it says the name. OK, so now this is now where we pass in the actual name of what that has been initialized to. So you can just say Philly. Now that is a string so freely. Few more. OK, so few more. Silvally So this is just a single name, OK. Now the idea is this. I can simply. Let me just make it more. Readable or understandable? OK. So I'm gonna also pass in the age. Then I'm also going to do the same. So self dot H. Then also age. OK. So that it actually makes sense so in this. Method called STR that stands for string, so I can also get that is at the place order those carry braces. So I'm going to pass in the edge. OK. So I'm I'm I'm now. I've actually added one. One variable, or rather just a property to this person or individual. So he or she. Is called the name on the edge OK? Ohh, so in now, now passing in the actual values to those properties so the name and then you can see now it tells us to say go to the age and passing anything OK to this person. So now age is a number, so there is no need for you to up to wrap that thing with the codes. So just going to say let's say maybe 24 or maybe 23. OK. And now? Once you've done is. This method. What it is just going to do is to print this object so that we can really understand what values of those variables are the properties these, the name and the age what how were they passed into that object. So now you're going to say. Just print now PC1. That's all we have to do, and then now we run the program, so the program will just return. The name is in how we passed it OK and then also the edge. And let me just put a pipe there. Maybe just this OK. So now I can run the program. OK. Now you can literally see that this is how we passed in the name, OK? We haven't actually touched any message like because this data is not meaningful to anyone that is looking at this data. So don't know what 23 means. We don't know what 3 months, what it means. It could be a name for anything. OK, So what has helped us to do this? Is this the method? This part OK? Because. When we actually disable this, let's comment this, OK? Now you understand that if you are just to print the object, there is going to give us some, you know, something that is not even readable, OK. Umm. OK, so you see what is you're just going to say, OK, this information is sitting on this location in your memory like if you're using the RAM or the, the, the room, whichever type of memory. So you can see that this pacing class okay object sits at this location. Now this location is not readable to human being, or at least it's not something that a human being can be able to understand. So they spatial method. The STR the string method this one allows us. Or at least helps us to understand how the data was passed into that object. Of the class. And we can see. The difference? So obviously that I'm able to return. Perfect. You know, data. Of course this is not 100% perfect, but it just tells us to say how the data was passed in OK. So that's how you can go about the special method in Python. The STR OK. You you just have to understand this. OK so here you use the keyword return so that here you are able to print it out what was returned OK. So thank you so much for watching and have a good day.To view or add a comment, sign in