If you're not using Python DATA CLASSES yet, you should 🚀

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments

Well, risky move pronouncing Geralt and Yennefer in a recording shared with nerds in public.

Great content as always! Really hitting the sweet spot of where I am as an intermediate.

👍︎︎ 17 👤︎︎ u/dukea42 📅︎︎ May 07 2021 🗫︎ replies

For small scripts I use dataclasses, for everything else there's pydantic.

👍︎︎ 9 👤︎︎ u/LightShadow 📅︎︎ May 07 2021 🗫︎ replies

Clarification: I'm using a sort_index variable in the example. Strictly speaking, that's not needed, because a data class uses the first attribute in the class definition as the default for sorting. I'm not a fan of this kind of hidden behavior, so I prefer to do it explicitly (using something that is called sort_index in this case). Another advantage of using a separate field, is that you can do more complicated ordering, using for example a weighted combination of age and strength.

👍︎︎ 8 👤︎︎ u/ArjanEgges 📅︎︎ May 07 2021 🗫︎ replies

Hi arjan , I totally liked ur video , precise to the point.just subscribed. If I want to create a model class why not just use named tuple ?

👍︎︎ 3 👤︎︎ u/mr-robot007 📅︎︎ May 07 2021 🗫︎ replies

Ah, I love his videos. He teached me a lot.

👍︎︎ 3 👤︎︎ u/Spaturno 📅︎︎ May 07 2021 🗫︎ replies

Can you use this approach to emulate SQL's "ORDER BY column_a ASC, column_b DESC, column_c DESC ...'? For those unfamiliar with SQL, the question is if you can order by a specified field, then in case of equal value compare based on another field (possibly with reversed ordering) and have an arbitrary number of such sorting conditions.

👍︎︎ 3 👤︎︎ u/JesterDBT 📅︎︎ May 07 2021 🗫︎ replies

I enjoy your videos, the way you explain the topics take my attention

👍︎︎ 3 👤︎︎ u/FabinhoHD 📅︎︎ May 08 2021 🗫︎ replies

Python and Witcher. What's not to like?

👍︎︎ 3 👤︎︎ u/amstan 📅︎︎ May 08 2021 🗫︎ replies

Like his accent

👍︎︎ 3 👤︎︎ u/noicenoice9999 📅︎︎ May 08 2021 🗫︎ replies
Captions
data classes in python allow you to write shorter code and initialize print compare and audio data much more easily get yours now for 20 off using the link in the description oh wait this is not a sponsored video and data classes are free i'm going to look at a few practical examples to help you get started right away and become an evil data manipulating overlord let's dive in if you're new here and you want to become a better software developer gain a deeper understanding of programming in general start now by subscribing and hitting the bell so you don't miss anything classes are a combination of two things behavior in the form of methods and data in the form of class attributes they're blueprint for objects they form the basis of object-oriented programming and developers use them in a million different ways some classes are mostly containers of behavior for example a class that allows you to draw all kinds of shapes on the screen or a class that provides password hashing functionality other classes act more as containers of data think of class for representing a vehicle in a vehicle registration system or a class for representing a polygonal mesh in a graphic system when working with behavior container class you might use things like inheritance to change the behavior or use design patterns such as the strategy you probably also won't have that many different instances of that class in your application a class that behaves more like a data container is often used differently you may need to create many instances you want to order them compare them easily inspect the data that's in them etc regular bare bone classes don't really provide a lot of useful functionality for such data-oriented classes that's why some programming languages provide a more data-oriented variant of a class for example c-sharp has a stock type that's much like a class but is more oriented toward representing data structures and since version 3.7 python has a data classes module so how are data classes different from regular classes first data class have a built-in initialize to help you quickly fill an object with data there are easy ways to print compare and order data you can create data that's read-only and they're totally free to show you how data classes work i'm starting with this very simple class person that has a name a job and an age person has nothing more at the moment than an initializer that allows us to create a person with those three values and here i'm creating three persons gerald jennifer and another jennifer you can never have enough jennifer's let's run this code and see what happens so you can see i print the ids of person two and three so that's what you see here and then when i print out person one we get person object at this memory address not very useful information doesn't tell us anything about what's inside the object another thing is that person two and person three are both jennifer they both are sourcers and they're both 25 years old if you compare these two objects the result is false because they are different objects that's when you're dealing with regular classes when you're dealing with data you prefer we want different behavior one thing you'd like is an easy way to print the data instead of having this person object at this address we'd like to see what is the contents of the object another thing is that often with data we want to do deeper comparisons so we know when the data is the same we consider the object also the same which is also not happening here data classes can solve all those things for us so let's turn person into a data class and see what happens so i'm going to import the data class decorator and add it to the person class because now person is a data class we no longer need this initialization method because the data class does this job for us so i'm just going to remove this note that it is important that you provide these type-ins here so dataclass knows what type of data it's dealing with and now i can simply run the same program and you're going to see the behavior is going to be very different let me run this again so now you see we get the ids of the objects they are different but when we compare them the result is actually true because they contain the same data also you note that we're no longer getting this person object at that memory address we're getting a nicely printed representation of the object that contains also the data so we can quickly see what kind of object we're dealing with those are the kind of things that data classes do for you automatically another thing you often want to do with data is comparing data for example like this unfortunately at the moment we cannot compare these objects with because we didn't define what that means with data classes you can define that they can be ordered and we do that by setting the order flag to true and now what happens is this so now you see we can compare these in this case person one is not larger than person two because also we didn't really define what that means if you want to define how to order objects you could add a sort index and that's an int and what we want to do for example suppose you want to sort by age now ideally you want to put somewhere that sword index should contain the age of the person so we can sort by that and the way to do it is in the post init method and the reason we need to do there is because post in it is called after init so the values have been set so we can do something like this the only issue with doing it this way is that sort index is now being used by the data class as a member of the class but we only want to use it for sorting if i try to run this for example you see that we get an error that it's missing a positional argument here because it's expecting a sort index which it's not getting in this constructor so the way to tell data classes that this is a field we're only using for sorting is by using the field function so we're telling data class that sort index is a field but we don't want to initialize it and now let's run this code and see what happens and there we are back again at what we are expecting and you can also see that now person one is deemed larger than person two because we're sorting by h so the result now is true one thing that's not so nice now is that if we print out the data you see that sort index is added here as well and maybe we don't want it there so we can add a extra option here to let the data class know that sort index shouldn't be part of the string representation let's run this one more time and then this is what we get data class also allow you to work with default values let's say we want to add a strength to the character this gives you a default value of strength hundred so if i run this again you'll see that this person here has strength 100 but i can also pass it as a parameter here and now the strength 99 so default values here are very useful so you don't have to initialize them and every time you create a person if we now want to change the sorting index we can very simply do that here by changing the value of h to strength and now we're sorting by strength another nice thing you can do with data classes is that you can freeze them and that's by setting the frozen value there's an issue though if i try to run this you see i'm going to get an error the problem is because it's frozen i cannot assign to the field sort index here obviously the only reason we're doing that here is because we want to be able to sort them but there's a trick that instead of assigning this directly we're calling the set attribute method and that looks like this now i can remove this line and it's going to have the same effect but we're going to circumvent the frozen setting from the data class and now our code works again as we expect and also because we're sorting by strength gerald is no longer larger than jennifer because the object is frozen i'm not allowed to set any attribute to it so if i try to do this we're going to get an error so frozen help you to make sure that data is not changed anywhere in your code so let me remove this because obviously gerald is not 12 years old anymore let's run that again so yeah we're back one final thing i'd like to show you is that there is also a nicer way to print out the data and that's with the underscore underscore string method this is not something that's particular to data classes you can also use it in regular classes but i think it fits nicely into this example so let's say we're going to return here a nice string representation of the person and now when we're printing the person we're going to get our nice string representation so overall data classes allow you to quickly create objects that represent data compare them order them and print them out in an easy way data has become the core of every software application and the amount of data we're consuming is mind-blowing in the beginning of 2020 the amount of data in the world was estimated to be 44 zettabytes which is about 40 times bigger than the number of stars in the observable universe on youtube people watch over a billion hours of video every day imagine all the comments and like data that that generates speaking of which like this video if you are enjoying it in the grand scheme of youtube it's meaningless but you made me very happy in short anything that helps us deal with data more easily is most welcome and data classes surely are a nice addition to python especially because they're free what i haven't really covered yet is software design and architecture aimed at better handling of data there's a lot to talk about ranging from design patterns for ingesting and transforming data to data manipulation patterns used in modules such as pandas data structures and architectures and more i'm surely going to cover those things in the future i hope you enjoyed this if you haven't joined my discord server yet here's the invitation link come over and say hello thanks for watching take care and until the next time [Music]
Info
Channel: ArjanCodes
Views: 114,575
Rating: 4.9665856 out of 5
Keywords: python data classes, python data class, python dataclass, python data class tutorial, dataclasses python 3.6, dataclasses python 3.7, data classes python, data classes, data class, python storing data, data-oriented class, struct c#, tutorial data class, data vs behavior class, python, python tutorial, learn python, python 3.9 tutorial, python 3.8, python 3.7, python 3.9.1, python 3.9, python 3.9.2, data classes python 3.7, data classes vs data
Id: vRVVyl9uaZc
Channel Id: undefined
Length: 10min 55sec (655 seconds)
Published: Fri May 07 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.