If you talking about the game, I bought it on gog but never got the chance to install it. Too much time fighting with k8s manifests and no time for playing :c
You've got to read the books, a masterpiece. If you liked the TV show or didn't, it won't matter after reading just the first two short story collection.
One cool thing about the "frozen" attribute is that if set to True, Python will automatically create a ___hash___ function for your dataclass, allowing it to be used in things like dictionary keys and sets. (Note that if the class contains an unhashable field such as a list, the ___hash___ function will throw an exception).
When dealing with data in Python these days, I find myself almost exclusively doing comprehensions and functional programming with dictionaries and lists, rather than using classes. But good to know this is out there.
I started using classes when I could not do otherwise. My fonctions had like 15 variables that I had to state explicitly. It was impractical. My code was working on molecules and mixtures of molecules. So I created a class "mixture" and a class "molecule" and suddenly my fonctions were acting on mixtures and molecules rather than long lists of variables. That was such a relief. Now my classes have like 70 attributes because, well, mixtures and molecules have many structural features and properties. Everytime I extend my code I simply add new attributes and that's it.
Where is 'love' button? 'Like' is not an accurate reflection of what I feel about your videos. I've seen many amazing dudes with online tutorials on CZcams, but you are the most amazing
This is something I was wondering to use in Python since I discovered that in PHP 8, you do not need to set the construct parameter to attributes anymore, you set the attributes directly on the parameter, this is very useful when you have a lot of initial parameters, pass the parameter and after that set it to an attribute is so so boring
8:43 "So frozen helps you to make sure the data is not changed anywhere in your code." ... right after bypassing this mechanism and showing how the data is changed in the code. :D Great videos, you are my no. 1 python instructor. Programming python itself is fun, but learning from a patient, calm, well explaining teacher is invaluable, but free. Thank you for sharing!!
Awesome! Coming from a C# background this really makes my day to know data classes are a thing in Python. Awesome. Really good video. Thanks Now I need to refactor all my custom data classes :-D
good video, for sort , dataclass will use all attributes by order for checking, so sort_index is not mandatory, its effected just because its the first one
It's funny that the main touted advantage of Python is that it's got dynamically typed variables. And now the first thing the seasoned programmer does is *hammer those variables down to be a single type to avoid issues further down the road* 😁
Dude I'm a pretty experienced developer but brand new to python as of 2 months ago... I've seen like 5 videos on dataclasses by now and this is the first one that actually showed the benefits over regular classes... after by mile long init function is finished lol
I've been coding for... hmm... since python 2.5 as an amateur, when I need it. The vast majority of what I learned is from back then, and while i've been keeping learning, in the last years, besides some modules here and there, I've not learned many new things... discovered the chanel last week, and man I've learned so much since ! Sure there are few design pattern that I was already doing out of pure logic and commodity, though even then the knowledge acquired here have allowed me to refine these to the next level (or on the road to it), plus the terms that goes with it ! Thank you so much ! And there are many new neat things that I've been missing, like the module abstractclass or this dataclass. A great channel : advanced and intermediate concepts, in python !
I do understand that you used sort_index to showcase field types, but wouldn't it be more intuitive to define sort_index to be a property? That way we ensure it's readonly and we also ensure the sort_index updates automatically™ as the relevant fields are updated.
All of your videos are still a little above my aptitude, I'm still relatively new to coding in general but I feel jumping in at the deep end can sometimes be good 👍
Hoi Arjan. Je maakt mooie video’s met duidelijke uitleg. Kun je een praktisch voorbeeld geven waarvoor jij data classes zou gebruiken. Ik zou denken dat, je die niet nodig hebt als je bv crud operaties wilt doen naar b.v. een SQL database
Hi Ronald, dankjewel! Dataclasses zijn erg handig in combinatie met Pydantic, aangezien je dan gebruik kunt maken van validatie van velden voor bijvoorbeeld APIs of database-modellen. Het is ook handig voor het representeren van een set config-waarden die je uit bijvoorbeeld een JSON file leest.
Thank you Arjan! Your videos about how to make more of Python (with built-in functions), write better and cleaner code, etc. motivate me in trying to become more Pythonian.
Another small thing: I see you using both single and double quotes. If you use a formatter (like Black) and set VSCode to Format on Save you’ll automatically get rid of these inconsistencies.
Seems useful and nicely Pythonic. Normally when folks say I should be writing classes, I tell them to go back to Java and leave us healthy people alone.
What happens if, for example, self.sort_index = self.strength in __post_init__, we instantiate a Geralt at some point in the code but change his strength to something else later? Does sort_index somehow get updated automatically?
I tried making a playing card dataclass but since the comparison is dependent on the tuple of all values I had to override the comparison operators or "Ace of Hearts is > Ace of Diamonds (H > D)". I'm really new to Python (and programming), I thought dataclass would be good for playing card class as it's just a data holder anyway but if I ended up implementing my own sorting is it still worth using? Overhead? It's such a simple use case maybe I'm over complicating things? I think my only benefit was the repr, str, and the init having done for me.
I've never been totally sold on data classes. To me, the functionality has been available forever in the form of dunder methods, @property decorators, and such. Am I being unreasonable here? Is there any sort of performance gain to data classes? Thanks for the excellent video as always!
Thanks! Actually, data classes are exactly the same as regular classes. The only thing the decorator does is already add dunder methods to make the class more suitable for dealing with representing data. So it's basically a shorter version of adding dunder methods yourself. Obviously, if what the data class decorator adds doesn't fit with what you need, then it is better to define those methods yourself, there is no particular performance gain to data classes.
I love your presentation. Keep it up. Im much less impressed with Python. Yes its powerful, but in my opinion the language grows in a very non obvious way. It starts with the @ notation to introduce what would probably have been a pure language feature (as jn class/struct) had they thought of it originally. Then towards the end you had to add weird, less readable notation such as setting attributes to overcome the weakness of the original extention. To an extent this is typical of the modern "Agile" approach which encourages starting with trivial stuff, without doing to much conceptual design, and adding stuff as your requirements evolve. But Agile recognizes that there is a price to pay, technical debt, which it encourages you to fix. In the case of a language or infrastructure you cant do that, you're committed to backwards compatibility and similar issues, so the language structure slowly decays, becomes more complex and you lose consistency.. I'm interested in your take on these comments.
Most of the programming channels show basic information with less relevant examples. I just came across this channel and let me tell you that you share the most relevant information I have ever seen on any channel. Thank you for your work, I really appreciate it!
Such awesome content, this video got me to start learning python and I'm loving it! Big request though, could you do a video about Flask? Would be much appreciated!
@@ArjanCodes Can you compare Flask with FastAPI too? Seems like FastAPI is faster and is more pythonic in way, but I am unsure of the drawbacks of choosing FastAPI over Flask. Can you share your thoughts?
quick question about the __str__ representation you added, why did you use __str__ instead of __repr__? I usually choose __repr__ when it doesn't really make sense that an object be casted to a string but I still want to print it (for debugging or just to display it for some other reason) and I use __str__ when I think there is a relevant use case for a string casting. But these were my own conclusions and a convention I set for myself, is this a good practice? Is there any reason to use __repr__ over __str__ (or vice-versa)?
This is a really cool feature and definitely useful for my work! Are data classes actually implemented differently at the c level or is this just a shorthand format for accessing commonly used features?
Very nice video, good explanations and well produced. When I see something like the frozen=True colliding with the __post_init then I get sad.... Coming from Java+lombok I am however, not really impressed with this dataclass annotation, but it did improve my knowledge of Python :) Keep the videos coming!
Could that sort order field be made as a python property? Do we get some shortcuts around using slots for packing these in memory when in large arrays?
I haven't used dataclasses yet, but this video does a good job of showing all the features of them! I am curious, can you add any methods to the dataclass that you want that can use that data to calculate something? Or are you limited to just dunder methods? If you can add your own methods than when do you stop using dataclasses and start using regular classes?
You absolutely can add your own methods to dataclasses. In the end, the dataclass decorator doesn’t change the definition of what a class is in Python, it simply sets it up to better fit a concept that represents data, including ordering, initializing with values, and so on. If you don’t need any of those things, then there’s no need to use dataclasses, simply use a regular class.
Hi Arjan, you have really great videos and great code. Can you do any video on database connection to python as OOP? Would love to see how you do it. Keep up the good work!
Anybody else into The Witcher?
If you talking about the game, I bought it on gog but never got the chance to install it. Too much time fighting with k8s manifests and no time for playing :c
I have the Witcher 3 for the Playstation lying in a drawer, haven't gotten around to that either.
@@ArjanCodes I have the Witcher 3 also lying in a drawer but mine is for the xbox :D
I thought the show had a bit of corny dialog but the more i watched it the more i was immersed and it didn't seem bad. so yeah i love it
You've got to read the books, a masterpiece. If you liked the TV show or didn't, it won't matter after reading just the first two short story collection.
Just in case you're feeling low Arjan, you are doing a freaking great job here. Thank you very much ! Keep them coming :D
Thank you so much for your kindness, much appreciated! I’m happy you like the videos.
2222²²²awaŕd
One cool thing about the "frozen" attribute is that if set to True, Python will automatically create a ___hash___ function for your dataclass, allowing it to be used in things like dictionary keys and sets.
(Note that if the class contains an unhashable field such as a list, the ___hash___ function will throw an exception).
When dealing with data in Python these days, I find myself almost exclusively doing comprehensions and functional programming with dictionaries and lists, rather than using classes. But good to know this is out there.
I concur with that!
This is the best way IMO. Heavy OOP often makes python code super messy as many people get involved in a codebase.
@@AddyVDH Agreed. Python is NOT supposed to be an object oriented language
I had a graduate class in LISP, i found the functional bits interesting and are now using them in python
FP un Python feels great !
Incredibly clear presentation. I've been programming Python for 25 years and hadn't used data classes. Thanks! Subscribed.
I believe it is fairly new to python
This is a really great intro to data classes, very clear and to the point. Looking forward to more videos like this!
Glad you liked it, Dennis!
This is great! Thanks for sharing Arjan. Love your work.
Love your videos too 🔥
I started using classes when I could not do otherwise.
My fonctions had like 15 variables that I had to state explicitly. It was impractical. My code was working on molecules and mixtures of molecules. So I created a class "mixture" and a class "molecule" and suddenly my fonctions were acting on mixtures and molecules rather than long lists of variables.
That was such a relief.
Now my classes have like 70 attributes because, well, mixtures and molecules have many structural features and properties. Everytime I extend my code I simply add new attributes and that's it.
Where is 'love' button? 'Like' is not an accurate reflection of what I feel about your videos. I've seen many amazing dudes with online tutorials on CZcams, but you are the most amazing
I would love to see a complete python course taught by Arjan!
This is something I was wondering to use in Python since I discovered that in PHP 8, you do not need to set the construct parameter to attributes anymore, you set the attributes directly on the parameter, this is very useful when you have a lot of initial parameters, pass the parameter and after that set it to an attribute is so so boring
Arjan, your videos are really clear and simple. You are very much appreciated. Greetings from the UK!
Thanks so much Liam, glad the content is helpful!
Really happy I found your channel! You and Corey Schafer are my two favourites for learning Python right now.
Keep up the excellent work!
Thanks so much Finah.
I LOVE U, THIS IS THE FIRST STEP FOR UNDERSTAND FAST API AND PYDANTIC,
THANK U FOR URS VIDEOS
8:43 "So frozen helps you to make sure the data is not changed anywhere in your code." ... right after bypassing this mechanism and showing how the data is changed in the code. :D
Great videos, you are my no. 1 python instructor. Programming python itself is fun, but learning from a patient, calm, well explaining teacher is invaluable, but free. Thank you for sharing!!
Awesome! Coming from a C# background this really makes my day to know data classes are a thing in Python. Awesome. Really good video. Thanks Now I need to refactor all my custom data classes :-D
It seems technical debt never really stops 😊.
I find the content of your videos are on the edge of my python knowledge. When I watch one, I learn something new, without it making my brain hurt!
That's a good way to put it! Ahah, I'm glad you're enjoying the content!
I find the asdict and astuple methods from the dataclass library very handy.
They are, I often use asdict to convert a dataclass to JSON :)
good video, for sort , dataclass will use all attributes by order for checking, so sort_index is not mandatory, its effected just because its the first one
Arjan keep doing your video man ! Your are a true teacher, everthing you say is just so clear. Thank you for your help :)
I'm glad my content has been helpful! Thank you for the kind words :)
It's funny that the main touted advantage of Python is that it's got dynamically typed variables. And now the first thing the seasoned programmer does is *hammer those variables down to be a single type to avoid issues further down the road* 😁
That HHKB. Typing was so fast and smooth.
It's a Keychron K2. Feels great.
Dude I'm a pretty experienced developer but brand new to python as of 2 months ago... I've seen like 5 videos on dataclasses by now and this is the first one that actually showed the benefits over regular classes... after by mile long init function is finished lol
I love how wisely you avoid all the issues I've had with other peoples code.
you're making me rethink my entire codebase. i wish i could start over now but i'm so far. sheesh
I've been coding for... hmm... since python 2.5 as an amateur, when I need it. The vast majority of what I learned is from back then, and while i've been keeping learning, in the last years, besides some modules here and there, I've not learned many new things... discovered the chanel last week, and man I've learned so much since ! Sure there are few design pattern that I was already doing out of pure logic and commodity, though even then the knowledge acquired here have allowed me to refine these to the next level (or on the road to it), plus the terms that goes with it ! Thank you so much !
And there are many new neat things that I've been missing, like the module abstractclass or this dataclass.
A great channel : advanced and intermediate concepts, in python !
Thank you, I'm happy you like the content!
I do understand that you used sort_index to showcase field types, but wouldn't it be more intuitive to define sort_index to be a property? That way we ensure it's readonly and we also ensure the sort_index updates automatically™ as the relevant fields are updated.
All of your videos are still a little above my aptitude, I'm still relatively new to coding in general but I feel jumping in at the deep end can sometimes be good 👍
Hoi Arjan. Je maakt mooie video’s met duidelijke uitleg. Kun je een praktisch voorbeeld geven waarvoor jij data classes zou gebruiken. Ik zou denken dat, je die niet nodig hebt als je bv crud operaties wilt doen naar b.v. een SQL database
Hi Ronald, dankjewel! Dataclasses zijn erg handig in combinatie met Pydantic, aangezien je dan gebruik kunt maken van validatie van velden voor bijvoorbeeld APIs of database-modellen. Het is ook handig voor het representeren van een set config-waarden die je uit bijvoorbeeld een JSON file leest.
That intro was hilarious. Well put video. Super clear.
Glad you liked it Alvaro!
This guy is awesome . The stuff i pick from you everyday is priceless. Got people at work thinking am a Python god
Best Python videos in the known universe!!
Thank you so much!
Thank you Arjan! Your videos about how to make more of Python (with built-in functions), write better and cleaner code, etc. motivate me in trying to become more Pythonian.
Glad to hear they’re helpful to you, Lucien!
Another small thing: I see you using both single and double quotes. If you use a formatter (like Black) and set VSCode to Format on Save you’ll automatically get rid of these inconsistencies.
In my more recent videos, I’ve started using a combination of Pylance, Pylint and Black - works really well!
I really like this! It feels like modern python is much more robust and can be better self-documented and typed nowadays.
You might also like Pydantic. It’s very similar but adds a few extras like data validation and nested models. It is a third-party package though.
Very straightforward and nice way of explaining how it all works.
Everytime watching one of your videos, I learn something new. Keep on going with these very nice and easy to understand videos :)
I've been using the conventional class in Python. This is such a great knowledge boost!!
Clearly demonstrated. Earned my “like and subscribe”! Looking forward to checking out your other tutorials/content.
Thank you Marcel - glad you liked the content!
Ahahahah, the opening where you're presenting dataclasses as if they're a sponsor, that's hilarious 😂
Thank you Pandu, glad you liked the video!
Great job. I love you explaining style.
Glad you enjoyed it!
Seems useful and nicely Pythonic.
Normally when folks say I should be writing classes, I tell them to go back to Java and leave us healthy people alone.
What happens if, for example, self.sort_index = self.strength in __post_init__, we instantiate a Geralt at some point in the code but change his strength to something else later? Does sort_index somehow get updated automatically?
"All your Data
are belong
to us" 🤣
+1 Arjan, love the Easter egg word portraits
loved Empire Earth as well xD
Arjan, you are great inspiration, love your videos!
I'm happy to hear you have been enjoying the videos!
Nice video Arjan ! Looks very useful
I tried making a playing card dataclass but since the comparison is dependent on the tuple of all values I had to override the comparison operators or "Ace of Hearts is > Ace of Diamonds (H > D)".
I'm really new to Python (and programming), I thought dataclass would be good for playing card class as it's just a data holder anyway but if I ended up implementing my own sorting is it still worth using? Overhead?
It's such a simple use case maybe I'm over complicating things? I think my only benefit was the repr, str, and the init having done for me.
Maybe check also the attr.s package.
I've never been totally sold on data classes. To me, the functionality has been available forever in the form of dunder methods, @property decorators, and such. Am I being unreasonable here? Is there any sort of performance gain to data classes? Thanks for the excellent video as always!
Thanks! Actually, data classes are exactly the same as regular classes. The only thing the decorator does is already add dunder methods to make the class more suitable for dealing with representing data. So it's basically a shorter version of adding dunder methods yourself. Obviously, if what the data class decorator adds doesn't fit with what you need, then it is better to define those methods yourself, there is no particular performance gain to data classes.
Can you make sort_index a @property so you don't have to mess around with mutation at all?
Oh my, that is a great product! I wish there was a way to pre order it though... It seems to ve impossible atm🤔
Why did I take so long to discover this channel? True goldmine of knowledge. Hit the bell.
Thank you Lean, glad you like the content!
In C# you can use records instead of structs for immutable data.
Good to know, thanks!
Damn. This information is going to save me years of time and frustration. Thanks for this.
I love your presentation. Keep it up. Im much less impressed with Python. Yes its powerful, but in my opinion the language grows in a very non obvious way. It starts with the @ notation to introduce what would probably have been a pure language feature (as jn class/struct) had they thought of it originally. Then towards the end you had to add weird, less readable notation such as setting attributes to overcome the weakness of the original extention. To an extent this is typical of the modern "Agile" approach which encourages starting with trivial stuff, without doing to much conceptual design, and adding stuff as your requirements evolve. But Agile recognizes that there is a price to pay, technical debt, which it encourages you to fix. In the case of a language or infrastructure you cant do that, you're committed to backwards compatibility and similar issues, so the language structure slowly decays, becomes more complex and you lose consistency.. I'm interested in your take on these comments.
I loved the start of the video :)
This channel deserves a million subs,the only channel most quality content :)
i take it that the existence of the setattr method "workaround" means that frozen variables should not be used for security purposes?
I think I'll revisit dataclasses in a future video. The setattr workaround is really ugly, so I'm going to figure out a cleaner way to do this.
Person1=("Triss) Person2=("Yennefer")
Print(Person1 > Person2)
TRUE
Just loving all of your videos man, keep up the awesome work. My focus is to watch ALL THE VIDEOS
Thanks, glad you like the videos!
You are my python role model
Video quality is insanely good for only 8K subscribers. Keep it up!
Thanks, will do!
Hands dowm the best tutorials on Python! Thanks for what you're doing! :)
Thank you so much!
Surely the sort_index should be specified as part of the dataclass decorator. Seems like an oversight to me.
Most of the programming channels show basic information with less relevant examples. I just came across this channel and let me tell you that you share the most relevant information I have ever seen on any channel. Thank you for your work, I really appreciate it!
Just found your channel and I am glad I did. Outstanding introduction to very useful concepts. Well done
Awesome, thank you!
Long since I subscribed. 😊 This is quite the top notch content.
Thank you so much!
Such awesome content, this video got me to start learning python and I'm loving it! Big request though, could you do a video about Flask? Would be much appreciated!
Thank you Josh - it’s on the list 😉.
@@ArjanCodes I hope with Dash...
@@ArjanCodes Can you compare Flask with FastAPI too? Seems like FastAPI is faster and is more pythonic in way, but I am unsure of the drawbacks of choosing FastAPI over Flask. Can you share your thoughts?
quick question about the __str__ representation you added, why did you use __str__ instead of __repr__? I usually choose __repr__ when it doesn't really make sense that an object be casted to a string but I still want to print it (for debugging or just to display it for some other reason) and I use __str__ when I think there is a relevant use case for a string casting.
But these were my own conclusions and a convention I set for myself, is this a good practice? Is there any reason to use __repr__ over __str__ (or vice-versa)?
Amazing! Thank you for creating this content!
Glad you enjoy it!
Arjan. I really appriciate your videos! Thank you! Really awesome
My pleasure, Patrik!
These are EXCELLENT videos.
Good twist at the start
Awesome tutorial bro!
I had used data classes but this is much more details that I didn’t know about.
Thanks
You’re welcome- glad you enjoyed it!
I have been programming in Python for quite some time now, and I had no idea of this functionality. Thanks :p
And this is how Python get a little bit closer to Haskell... Nice!
But then why don't we just use Haskell?
I don't remember any programming tutorial this good. Even K&R. And I started in 1974. I guess it could be memory loss due to old age.
Thank you so much, glad you're enjoying it!
This is amazing! I’ve been struggling with my project and this totally saved my day!
Glad I could help, Elvis!
This is a really cool feature and definitely useful for my work! Are data classes actually implemented differently at the c level or is this just a shorthand format for accessing commonly used features?
Great video though I am struggling to find my own personal use cases. Seems unpythonic in syntax to me?
Very nice video, good explanations and well produced. When I see something like the frozen=True colliding with the __post_init then I get sad.... Coming from Java+lombok I am however, not really impressed with this dataclass annotation, but it did improve my knowledge of Python :) Keep the videos coming!
Thanks for the video. I just learnt something!
Glad it was helpful!
Wow! This video was super helpful in getting familiar with data classes! Great intro! ...Subscribed!
Awesome! Thank you!
Could that sort order field be made as a python property? Do we get some shortcuts around using slots for packing these in memory when in large arrays?
I haven't used dataclasses yet, but this video does a good job of showing all the features of them!
I am curious, can you add any methods to the dataclass that you want that can use that data to calculate something? Or are you limited to just dunder methods? If you can add your own methods than when do you stop using dataclasses and start using regular classes?
You absolutely can add your own methods to dataclasses. In the end, the dataclass decorator doesn’t change the definition of what a class is in Python, it simply sets it up to better fit a concept that represents data, including ordering, initializing with values, and so on. If you don’t need any of those things, then there’s no need to use dataclasses, simply use a regular class.
maybe use pydantic in that case. Is like a dataclass but more potent
Very useful, thanks!
I came from Java to Python and I feel that i have only arrays and dict . Good tip for People like me.
That "ALL YOUR DATA BELONG TO US" in the background was funny. Nice quality content btw :)
Thank you!
Thanks, great job!
Very confusing that class variables are used as if they are instance variables, or something like that. What am I missing?
I thought you could set frozen on certain fields as well?
Loved this!
Very good! Thank you. You really have a talent to explain things
Couldn't you also do __gt__ to do the comparison instead of the sort_index?
Hi Arjan, you have really great videos and great code. Can you do any video on database connection to python as OOP? Would love to see how you do it. Keep up the good work!
Thanks Senga, happy you’re enjoying the content!
Thanks for the suggestions, I've put it on the list.
very clear, good length, witcher references. what's not to like? subscribed :)
Welcome aboard!
It would be interesting to know if a high speed camera could catch your keystrokes 👀
And what about attr lib?
This is very similar to dataclasses but has validators and other stuff that seems to be better.
Seems very similar to structs in C++.
not really, c++ structs are identical to c++ classes. The only difference between the two is the default access modifier
*laughs in C*
Thank you sir, brilliant!
Glad it was helpful!
10/10 video, subscribed and liked 😀
Thank you, glad you liked it!
Awesome video! Thank you!
You're welcome AJ!