How To Use The NEW "Self" Type In Python 3.11 (NEW FEATURE!)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
wow recently we went ahead and covered two of the new features that came out with python 3.11 and that was the task group and the exception group now it's time to jump into the self-type which is a new type they introduced in Python 3.11 so I just want to go over why we should use it and where it's useful so the first thing you need to do to use this self-type is go ahead and import from typing the self keyword and to explain why it's useful for the first example I went ahead and copy and pasted the literal example from the pep docs and I'll leave that in the comment section down below so you can look at that as well but the pep talks didn't really give a working example so I had to go ahead and edit it slightly just to show you exactly why you'd want to use self the concept might be clear it's just on pep for some reason I could not get the code to work so I had to play around with it to make sure I could explain it but let me go ahead and explain to you what this code is it's taken directly from the pep docs so here we have a class of shape and it only has one method which will set the scale of the shape and then here we actually use it and it's going to return to us a shape as soon as we set the scale because it's returning self which is the shape of course now that we have a shape defined we can go ahead and create another class called shape and it's still going to set the scale except this time it's going to return a shape since now we have that defined so we can do the same thing as earlier and it's going to return to us a shape now where things become interesting is when we create a new class A Class such as Circle and here we want to set the radius first Circle and what we want to do is return the circle so naturally we will try to return self which should refer to the instance of the circle now where the problem arises is when we go ahead and instantiate circle use the dot set scale method and that's going to return to us a shape as we have defined up here as soon as we use set scale it's going to return self which is the shape and we don't want that to happen to Circle we want to return a circle so we can continue using the methods of circles such as set radius because as soon as we call set scale you'll notice that we cannot use set radius anymore because this does not belong to the shape instance this only belongs to Circle so if we have a circle we can easily set the radius and that will work fine because that returns a circle but as soon as we set the scale we won't be able to use any other methods associated with circle and again that's because we explicitly told the program you need to return a shape and the way to work around this is to go ahead and use the new self-type because by returning self you're returning the instance of whatever object used it so if a circle uses set scale it's going to say this should return a circle and self is going to be the circle so this will work as normal set scale can now be a shape or it can be a circle because it's going to return whatever object actually used it and this was the example that we got from Pep so it helped with inheriting methods and returning the correct type depending on which object we used so that was the major example given by the pep docs but there are some other uses so if we import from typing once again the self-type we can now explore some other examples on how you can use it for the next example we will create a class of fruits and it will have an initializer that just takes a name of type string and we will set the self.name to be that name just so we have some funky code that creates a fruit now an example given by the python documentation was with the class method so if we create a class method and here we create an alternative Constructor such as create super fruit and that's going to give us the class and we need to provide a name of type string and the power of type integer now here we can return self which is going to be the class instance itself and when we type in super fruit and say that's of type string and that will equal the formatted string of name and another interpolated string with an exclamation mark times the power so some funky code again and we return the class with the super fruit we will now be able to check that we are returning self and this is cool because now also if we change this to something such as Apple we're not going to have to change this to Apple as well we're not going to have to make any changes here because self is always going to refer to the class so now we have a way of creating a super fruit and that was one way to use it and I'll show you later how we can use that but next if you are familiar with context managers it was also very useful if you wanted to create an enter method and if you wanted the enter method to return the instance itself then you can go ahead and type in return self and here you can do your processing and then return self and once again if you are working with inheritance it's very nice to yourself because if we did something such as def change name and we have self in there and we have the new name we want to edit we can just return self instead of returning a fruit and that's going to help us a lot with the context options for the other class because if we have a class of orange for example and we change the name with a method from fruit we don't want to get a fruit back we want to get the orange back so we can continue using the orange methods so self dot name dot name would equal name and we would return self so we can continue processing that object now the rest works as normal if we create a fruit of type fruit for example we can type in fruits and we will just type in create super fruit so here we can type banana and we'll make it super so we'll just add 10 and that's going to create an instance of fruit with our special Constructor over here which returns self and that is great now the ambulance is here because the banana is about to commit suicide now if we type in print fruits dot name and we run this program we're going to get our Superfruit back but to sum it up we now have an option for returning self if we need to earlier we would have to go ahead and use the type bar to create whatever type we wanted but now we don't have to do that anymore because it's built into python 3.11 so it's just a major convenience for us in case we want to respect inheritance and make sure we have the right methods when we're working with certain classes but that's really all I wanted to cover regarding the self type and in case I missed anything please do let me know in the comment section down below I would love to read about it all that being said thanks for watching and I'll see you in the next video
Info
Channel: Indently
Views: 16,295
Rating: undefined out of 5
Keywords: pyton, pyhton, pythn
Id: MBwLv5cY-Bk
Channel Id: undefined
Length: 7min 17sec (437 seconds)
Published: Mon Nov 28 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.