Data Classes in Python Are The New Standard

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] what is going on guys welcome back in today's video we're going to learn about data classes in python so let's get right into it all right so we're going to talk about data classes in python today a concept and a feature that was introduced with python 3.7 uh it's not the newest feature but it's very useful and very interesting nowadays whenever we implement data based or data focused classes we want to use data classes that's the best practice and the modern way to do things the go-to way to do things and database data focus basically means we have a class that is mainly there to structure and store information or store information in a structured way for us for example what would be a data class or a database class data focus class would be something like car where we basically just have a manufacturer and model name or whatever or another data class would be person we have name age weight height gender whatever another one would be computer where we have some information about the computer so these are all classes that are used to store certain fields so we have a field name age weight height and we want to have this class to store these informations to represent them to be able to compare them and all that that is the goal of uh database or data focus classes in contrast to that we also have uh more functionality-based functionality-focused classes uh for example classifiers if you use scikit-learn you have regression objects that are based from a regression class or that are an instance of a linear regression class for example or a classifier class or in the hashing module you have hashing classes and you have in the xml module you have parsers and all that these are all classes and objects that are used for their functionality we don't use them to store name age height whatever we use them to call functions to do certain things and to get certain results and whenever we use database classes we can use data classes to make everything more effective more efficient more professional and less prone to errors so let me show you an example right away let's say we have a class called investor here and we're not going to use data classes first now if i want to have an investor i want to have an investor that has name age and a cash level in a web app for example let's say this is a big application and i want to create this investor class and i want to have in a database also an investor table and all that and i want to map everything later on and so on and what i want to do now is i want to have this investor with name h and i want to have a cash amount that this investor has ready to invest into assets and securities so what i can do is i can just create a simple constructor here self name age and cash i can also to make it more professional provide type hinting here so integer and float for example and then i say self.name equals name self.h equals h self.float equals float like that um oh selfie float is not right self.cash equals cash sorry i was brain afk um there you go so this is a basic constructor now what can i do with this investor class let's go ahead and create an investor 1 i1 is going to be investor we're going to call this guy john john is going to be 25 years old and he's going to have like 9 000 in cash now let's go ahead and create two more investors let's say i3 i2 and now i have i don't know anna anna is 30 and has like i don't know 12 000 and then we have bob and bob has bob is like 70 and he has like 800 000 ready to invest now what can we do with these objects by default of course we have the constructor because we have created the constructor this is why we can even call the constructor here but besides that what happens if i print i1 what happens if i do that um let's go ahead run this we're going to see main investor object at a certain memory address so we don't really get any information we don't get a representation and those of you who know the basic um double under methods that we have here you know that we can change that by just implementing the representation method here so we can say represent and then i can say okay return i don't know name is self dot name and whatever and i can do all that let's just say we want to have the name we don't want to have h or anything else we want to only print the name by doing that we get this as a result now so i have to manually implement the representation now what happens if i want to know if uh for example let's create a fourth one let's say i4 is investor and it's going to have the same fields it's gone it's going to be john as well it's going to be 25 years old and also 9 000 in cash and now i want to print i1 equals to i4 is that true or not are they the same or not it says false okay uh so basically it will only say true if i say i1 equals i1 it's not gonna say okay those are the same fields so they're equal uh because we don't have an implementation of the equal method so i could go ahead and say okay underscore underscore eq self other and then i can say okay return self.name equals name so this of course that other name sorry um this of course would mean that two investors are equal when they have the same name doesn't make a lot of sense in this case but in this case even if i change the age here to 50 and i change this to a hundred dollars and then i compare i i one with i4 then i would get true because we have manipulated the equation method the equals method and of course we can we can do all that professional we can say okay if all the fields are the same we can decide what we want to do um but the fact is that there's no default functionality so if i say for example i1 being less than i4 and by that i want to say okay does this person does this investor have less cash than this investor uh it's going to give me an error message because the type is not supported so what i could do is i could go ahead and say okay less than implementation self other and then i could go ahead and say okay return if self.cash is less than other dot cash and i could run that and we would get false because that's not the case now what happens if i turn this around and i say greater than oh sorry i opened the settings greater than it says true okay so you can see that we can do all that but we have to do it manually and the problem is that if i now change something and this is just the beginning right we can do a lot of more things we can do uh stuff like hashing and stuff like uh very many other methods that we can use here uh but in the end if i change something here so let's say i want to also add a list of assets or i want to have i don't know uh a birthday or anything any additional field that i want to add here i would have to go through all the functions that i already have here all these less than equals hashing representation all the different functions and i would have to i would have to change everything and if i forget something it is prone to errors and all that so this is not the best practice way to do it nowadays and we're going to use data classes to make this more professional and less prone to errors so we're now going to implement the same investor class but this time using the best practice way of data classes and for this we're going to start by saying from data classes import data class and data class is basically just an annotation a decorator we can add to a class to make it a data class so let's say we have class investor again we have a placeholder pass and up here i can now add at data class and this makes it a data class what does that mean now it basically means that i don't have to provide a constructor i don't have to provide an equals method i don't have to provide a representation method it's all going to be done for me it's all going to be done for us all we need to do here is we need to provide the fields so for example the investor has a name and the name is a string so name colon string has an h integer and cache float like that that is the investor class now it's a data class and as you can see there's no constructor there's no representation method there's no equals or anything but i can still go ahead and say i won equals investor and the investor is john john is now 80 and john has 700 bucks on his account and i can go ahead and say print i1 and when i run this here by pressing run main you can see that i get a nice representation here and i can use this constructor even though i have not defined it now if i remove this data class here it's not going to work anymore it says unexpected argument if i run this you can see takes no arguments so we need to use the data class annotation here to make this possible now also if i add a second one here i2 equals investor and then i have like mike and mike is 18 and mike has like i don't know 2 000 on his account and if i now say i1 equals i2 it's gonna say no false but if i have a third one for example and i say this is also john and john is also 80 and john has also 700 bucks on his account and if i now say if i1 is the same as i3 it says true even though this was not the case if i were not using a data class because then it would only return true if it's the same object so this is also already implemented for us and the good thing is again if i want to add a field if i want to add a new field here for example i don't know uh favorite favorite sport for some reason and this is a string everything is done for me so i can of course add a new field here and i can say soccer and i can say baseball and i can say basketball whatever i now have the same behavior here so i have the comparisons i have the printing i have uh the constructor fields i only need to add one line to make all this possible i don't have to go through all the functions with a chance of missing something and making an error i can just add this new field and everything is done for me now what if for example i want to add a field but i don't want to represent it first of all what you can always do is you can just go ahead and overwrite the representation function just because it's done for you doesn't mean that you cannot override it so if i say for example repo wrapper self and then i return hello here it doesn't matter what the data class does i can still go ahead and just print i1 let me just remove the soccer here and the basketball and the baseball in the basketball there you go if i now run this you can see hello because that is the representation function that is called so i can still override it but there is of course a better way to do this so all we do here is we manipulate the individual fields and in order to do that we need to import something called field this is a function that we can use and if i want to set certain things for a field i can just go ahead and say for example for cache if i don't want to print the cache whenever i print a full object i can just go ahead and say equals field and then wrapper equals false this means that when i call the representation function cache will not be included so if i run this now you can see name john h80 but no cash there i can do the same thing by the way with uh the the comparison so i can say okay when we compare to objects uh don't care about the cash so compare equals false for example and if i do that now and we have john and john both are 80 years old but they have different cash levels so 700 and 900 dollars then i1 and i3 is going to be the same even though they have different cache levels but if i don't add compare equals false by default it's true then it's false because the cache is different so i can include and exclude fields as i want i can do the same thing by the way with init so i can say init equals false and then i don't have to specify where i can't even specify the cache in the constructor uh so i can set all sorts of things here in the in the field function here to make the behavior of the data class change and of course by the way i can also just set a default value so i can say default cache is going to be zero so i can actually let's do it without spaces i can say okay um well let's do it 0.0 i can say okay i can provide a cache cache level if i want to let's go ahead and set this to true and print i1 i can set a cache level and it's going to be printed but if i choose to not provide a cache level it's just going to go with 0 as a default value whereas if i don't provide that keyword here it's going to say that i'm missing a parameter here so if i run this you will see that missing one required argument so we can manipulate these these fields like that now let's go ahead and make our data class even more powerful by adding the order keyword because right now if i go ahead and say print i1 is greater than i3 i'm not going to be able to do that because this is not supported for this class so i can run this type error not supported for instances of investor and investor so what we can do to change that is we can just go ahead and say order equals true in the data class decorator and when i now run this we're going to get some result at least so i1 being larger or greater than i3 there you go you can see it's false in this case but this result is not exactly what we want because it just takes the default way of comparing objects what we want to have is in this particular case we want to compare the cache only so we want to say okay i1 is greater than i3 if i1 has more cache than i3 and vice versa same goes for less than obviously so what we want to do here is we want to have a custom way of sorting a custom order and in order to do that we need to add a so-called sort index so i need to go ahead here and say sort index is going to be a float and it's going to be a field that won't be initialized because we don't want to set it manually so and it falls and we also don't want to represent it it's just something that works behind the scenes and this sort index is it's what is what's going to determine the order of the object so this is what's going to be used for sorting primarily and in order to set it what we're going to do is we're going to set it after the initialization process so after we have the values for cache age name and so on we're going to set it to the cache value so we're going to call the uh post init function we're going to use the post init function to say once the initialization is done and i have a value for cash say self.sort index equals self.cash like that and now we can go ahead and see what happens if i say print i1 is less than i2 and if i run this now not debug come on terminate if i run this now you will see true because 900 is less than 2000 if i change this to 2001 this is false if i change this to 1999 it's true again now the only thing is that if i change this to 2000 2000 uh often times it's going to say true this is because the equal function is a little bit different here but for basic stuff you can see that this works and this also works when it comes to sorting a list of investors so if i have a bunch of investors here let's say we have mike john anna uh and anna has like a thousand mike has four thousand and add a couple more here four and five and we have bob and we have uh i don't know uh charles they go 70 years old and 18 years old and here we have 800 000 and here we have 100 bucks or actually let's go with three thousand bucks so we have these investors here and we can say we have a list my list is going to be i1 i2 i3 i4 i5 this is the basic list in that order and now i want to sort this list so let's print or let's say my list dot sort and then print my list if i do that you're going to see that they are sorted by cache levels because that's the default comparison method as you can see so we didn't have to do anything we have to specified sort index so basically say okay what is the sort index we want to sort by cache but besides that we didn't have to implement any uh functions like the constructor the ordering the comparing the representation everything is done for us and this is also the case for hashing if i want to be able to hash in the investor class by default i can either make this a frozen class so i can either say frozen equals true and this would make the class immutable and also add a hash function or i can also say unsafe unsafe hash equals true which makes the class mutable but also has a hash function so this is of course insecure because hashing doesn't make a lot of sense if we have mutable objects or at least it's unsafe but by doing that i can just go ahead then and print hash off i1 and hash off i2 and the good thing is that we're going to get the same hash for the same object so if i have uh let's say this is now i 9 for whatever reason so we have i1 and i9 have the exact same values if i hash both of them we're going to see that they have the same hash value as you can see here now if i change something for example the balance here to 200 instead of 2000 they're going to have a different hash however i can go to the field which is the cache and i can say hash equals um false but you're going to see that the the that the hash is going to still be different and the reason for that is because the sort index is uh the cache so we need to say hash equals false here and then we're going to ignore these two fields for hashing and we're going to produce the same hash as you can see so that's it for today's adobe and journal if you learned something if so let me know by hitting the like button and leaving a comment in the comment section down below and of course don't forget to subscribe to this channel and hit the notification bell to not miss a single future video for free other than that thank you much for watching see you next video and bye [Music] you
Info
Channel: NeuralNine
Views: 8,327
Rating: 4.977695 out of 5
Keywords: dataclasses, data classes, data class, dataclass, python dataclasses, python data classes, dataclasses tutorial, tutorial, guide, python, best-practice, data-driven, data-based, decorator, annotation, python dataclass tutorial, python datacalsses tutorial, what are dataclasses
Id: ojrbuVKblew
Channel Id: undefined
Length: 20min 33sec (1233 seconds)
Published: Tue Aug 24 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.