python: NewType vs aliases (intermediate) anthony explains #368

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello and welcome to another video in this one we're going to be talking about new type and how it relates to aliases and how you might use it in your program let's jump into it all right so what new type is is a way to create a kind of typing time only fake type that represents a strict subclass of your particular code uh since we're working with typing we're gonna set up my pie and we're going to show some small examples um okay run my pie over here okay so for new type you can do from typing import new type and the way new type works is you construct it very similarly to either a type variable or an alias you give it some sort of name let's say we have a business id for example and a new type takes a type as or the type name is the first argument so it's always going to be you know the same same name as what you're assigning it to and then the second argument is the actual subclass of what you're going to be using so let's say that a business id is always an integer and what this kind of does what you can think of it as doing is making something like this [Music] or at least that's how it appears to the type checker so it creates a kind of imaginary subclass that can allow you to constrain your apis to only take particular types but this can be substitutable anywhere else and it actually has no over runtime overhead so if you do from typing import new type and t equals new type e let's say int oops if we call t on this it's actually just going to return back the original value so they are referentially equal this is just a a pass-through function at runtime it doesn't actually do anything it does have a tiny teeny tiny amount of overhead because it has to call this function but it it generally has no runtime overhead behind beyond that but what this allows you to do is it allows you to define apis that specifically take this uh special subclass of your integer type and disallow other random integers at runtime so we may you know have something like this and without new type you would just say ins and you know maybe this does some sort of look up uh i don't know let's say stir and it does like i don't know to do some database lookup uh let's call this business name or something um etc and you know we're not gonna actually fill out the the implementation of this but you can imagine that this would take a business id and look it up in the database and turn back the name but our type here is just integer and so you may accidentally pass in some value here that you don't expect to be the right value maybe it's like an index or something and like you call business business name on i don't know for like you didn't actually have a business id and so calling this uh api is not exactly what you would want unfortunately if you well right now if we run this my pi is going to be like yeah that's fine no no issues here but if we would have specifically typed this as a business id um mypay is going to prevent this call here because it has an incompatible type like we we force this to have this our special new type type and we can't get anything else out of that and you might you know of course have like lookup business by name which has some stir and normally you would have this return int but since we have our own special business id uh we can implement our own logic here so let's say that we had somehow gotten uh this from our database and it had you know id or and name my business or whatever it is uh but pretend pretend this came from the database now you actually want to do is if we just return this directly business id uh oops i want to do that um oh actually let's pretend that we had uh because my pi is inferring that this is dictionary stir to object because it doesn't actually see the rest of that so we're just going to pretend this is a named tuple real quick um but maybe we got from our database business names so let's say that we got this from our database business id equals four name equals my business and maybe this is like a sql alchemy object or something else like that and not just the fake name tuple that i've done here if we did this return here we'll notice that my pi is again going to complain because we've returned just an integer here and not our actual business id type and the way we convert into our specialized new type is by calling the new type there so now this this will force this value to be this particular type we are allowed to convert from the integer uh the type that new type specifies into this value because it basically acts as a subclass and at runtime it's not going to do anything different so this is this is always going to return a business id the other cool thing that this lets us do is because this is a subclass of integer it allows us to constrain what sort of operations you can do on it so if we did business one equals lookup business by name foo and we looked up another business by bar we can't do nonsensical things like uh print business one plus business two like there's adding two business ids together it doesn't really make sense uh wait why does my pie let us do that wait it's not supposed to let us do that uh i guess if you um i don't know you should get that other oh i know what what it'll last to so if we do get business name uh business id business id stir that same function that we had before if we did get business name here this is not gonna work i think yeah there we go uh because the addition here is going to give us it's going to promote it back to an integer uh and not our special subclass here and so you know doing nonsensical operations on this can can be forbidden in ways um and most most of the use cases that i've seen for this is to have specialized types that are kind of segregated from their actual types and uh allow you to constrain apis like this um in other languages well i don't actually know if this was just something a co-worker told me but i think in other languages they call this tiny types or at least that's that's what they told me so if you've heard if you've heard the name tiny types this is basically an implementation of tiny types now what i wanted to talk about here is the differentiation between a new type which is you know explicitly making a subclass and an alias so if we had done an alias instead where we didn't have new type here my guy's not going to complain about any of this here because it's essentially just substituting int in here rather than our specialized subclass so we don't we don't actually differentiate our type here at all aliasing is mostly just used as a shortcut for spelling out the long type whereas new type gives us a distinct separate type from the original type uh is there anything else i think that's all i wanted to cover that's new type as well as the difference with alias hopefully you found this interesting if there are additional things you would like me to explain leave a comment below or reach out to me on the various platforms but thank you all for watching and i will see you in the next one you
Info
Channel: anthonywritescode
Views: 536
Rating: undefined out of 5
Keywords:
Id: 9rRYeunzX8c
Channel Id: undefined
Length: 8min 16sec (496 seconds)
Published: Wed Dec 08 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.