Python Has A NewType That No One Is Using?

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in the typing module we have something that's called a new type but what is it and what does it do so to show you I'm going to be creating an example that doesn't use it and then I'm going to be using the exact same example except with the new type so right here we're going to get started with some type aliasing and type aliasing means you're just giving your own name to a type that already exists for example you might have something called a user ID and that's just going to be the type of integer of course this can be much more complex this can even be something such as a list of integer or it can be a tuple of type string and float it can be anything you want we're just creating an alias to this type here so we can use it with our own name anyway once we have that we can also use it just by calling user ID and inserting the number of 10. this is going to be treated exactly the same as the int type so if we print this it's not going to be any different than just using integer but again if this was more complex type aliasync can make it much more readable anyway let's create a function called find user and it's going to take a user ID which will be of type user ID and all it's going to do is print that we found the user ID so to call that it's pretty straightforward we just need to enter a user ID and here we're going to search for the user ID of 10 and if we run this it's going to work just fine but there is a problem and that is that we can also just insert an integer such as 100 and that will also work but that kind of makes our type here quite useless so with the new type we're now going to solve this issue and new type is useful for making a subtype so let me show you exactly how that works so instead of creating an alias that directly links in to user ID we're going to refer to the new type keyword and this time we're going to to say okay the type is going to be called user ID and it's going to be a subtype of integer but you can put whatever type you want in there however complex it may be and I will remove these two lines of code for now but what you should notice when we print this line of code is that we're still going to get 10 back nothing has changed so far this is still treated as an integer but this time when we try to call our function we're going to have some different behavior from our code editor for example if we try to type in find user and we pass in something such as 10. it's going to say hey this is not the correct type we're expecting a user ID even if user ID is a subtype of integer it's not going to be happy with us putting in an integer we're going to have to wrap this by user ID so now we have to be quite explicit with what we're passing in and if we wrap that in user ID everything is going to work as normal as you can see we found the user 10 and if we actually go to the documentation you'll see that new type creates simple unique types with almost zero runtime overhead so it's also incredibly efficient since it doesn't really create anything new it just makes our type more unique because without this the user ID would be exactly the same as just saying input the value of 10. also note that if we try to find a user and we say Okay user ID and we insert some text that's also going to give us some syntax highlighting but again if we change that to something such as an integer it's going to work just fine so this is quite effective when it comes to typing and once again this type may be unique but the value is not this is still considered an integer when we run it and to demonstrate that I will print the user ID with the value of 10 is equal to 10. and when we run that we will get true as a return because all this does is simply create a unique type it doesn't change the type it doesn't do anything to the type it just creates a sort of wrapper that can be used with type hinting anyway I would love to hear what you think about the new type in Python do let me know in the comment section down below whether you think you will use it or if it's just a waste of time I would love to hear about it but as always with all that being said thanks for watching and I'll see you in the next video
Info
Channel: Indently
Views: 45,483
Rating: undefined out of 5
Keywords: pyton, pyhton, pythn
Id: ZdUGor8UkgQ
Channel Id: undefined
Length: 4min 24sec (264 seconds)
Published: Sat Jul 29 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.