Python dataclasses will save you HOURS, also featuring attrs
2021 ж. 3 Сәу.
855 175 Рет қаралды
Get rid of boilerplate writing classes using dataclasses!
In this video we learn about dataclasses and how to use them, as well as the related attrs library that dataclasses were based on.
Note: As of Python 3.10 dataclasses DO support slots.
― mCoding with James Murphy (mcoding.io)
Source code: github.com/mCodingLLC/VideosS...
attrs library: www.attrs.org/en/stable/examp...
SUPPORT ME ⭐
---------------------------------------------------
Patreon: / mcoding
Paypal: www.paypal.com/donate/?hosted...
Other donations: mcoding.io/donate
BE ACTIVE IN MY COMMUNITY 😄
---------------------------------------------------
Discord: / discord
Github: github.com/mCodingLLC/
Reddit: / mcoding
Facebook: / james.mcoding
A "Production Python Code" course by you would be a hit. These are the things I never picked up as a mostly self taught programmer. Great stuff.
Thanks!
I also think this would be cool, but would be hard not making it too long while staying informative. It seems like many KZhead channels favor having a 5 hour crash course. Beginners don’t have the attention span (imo) because they’ll have so many questions.
@@logankillen2669 this isn't beginner content, it's intermediate content for programmers who want to improve.
@@johnr3936 which are the hardest tutorials to find!
This would be actually what I need ! Focus on real world professional prduction-code. Either to be better qualified for a job or to particiapate in big projects like pandas, numpy etc.
I love that you show WHY you would want to use this rather than just what it is. It really drives home the actual usefulness of this
I think that's what's really the important part.
Totally agree! Happy KZhead recommend me your channel!
Thanks! The WHY is the part that is missing almost everywhere else. Subscribed and hoping for more :-)
Agree 100%
Agreed! Context matters.
Update: as of python 3.10, slots are now supported in in-built dataclasses with the ``slots`` argument
what is that?
I have been looking for such tutorials (unique less known concepts for intermediate programmers) for years now. This channel is an absolute gem! Great work James!
Thanks so much for your kind words!
Best Python videos on youtube. Or maybe most valuable? Covering so many useful and unique topics no one else seems to.
Wow, thanks!
@@mCoding thank you very much for your videos. I know something new every video.
I'll remember this. I don't know much about OOP so I didn't understand much. When I'll learn, I'll re-watch this video.
You don't need to know about OOP to understand classes! You are simply making your own class that holds an int and a str!
@@mCoding Classes are the very basics of OOP. If you don't know about OOP, chances are you don't know how classes work either.
You definitely need classes for OOP, but classes can be used for other programming paradigms besides OOP, though I guess you are right in that usually classes are introduced in the context of OOP.
@@mCoding Yeah, I have seen classes used in programs, where no OOP was involved. In those programs, classes were used just to pack data into one structure. However, I've not seen @property, __hash__, or how to make them immutable so I thought they were covered in OOP.
I strongly recommend that you learn Python and OOP immediately.
Thank you for this valuable video! Before watching your video, I assumed dataclasses were just a Python version of C structs, for cases where you didn't want to add functions in a class but didn't want a raw tuple either. After watching your video, I learned that dataclasses are much more sophisticated than that.
Glad it was helpful!
I don't think my jaw has ever dropped from a coding video before. The beauty of being an engineer is you learn something new every day. So much hidden utility in python its amazing!
Seen a few Python dataclasses videos but this one is the clearest and most informative in a short time. Well done!
I have just started experimenting with Python to use animation libraries from Grant Sanderson / 3Blue1Brown and have seen your videos pop up. Your organization and presentation of these tips is great for both initial learning and reference. Very concise, straightforward examples without cryptic foo / bar based variables, etc. New subscriber!
Thanks for shining a spotlight on these very useful Python modules with a good, concise tutorial. Great comparison between attrs and dataclasses, too.
Thank you for introducing me to dataclasses! Using a dataclass has just considerably simplified a module that I'm working on right now.
I love the way you start by explaining and go to the solutions. Most people just start by showing what it does and how to do it, without ever mentioning why we really need it. Keep going, your style of teaching will reach millions of people.
Yes
These are really great videos! I'm just learning Python for work and they're extremely helpful with simple and clear explanations.
Cool functionality. Thanks for sharing @ 3:32 the dead pan to the camera... Lol ! Love the dry humor
I will slowly dial up the humor moving forward.
@@mCoding its absolutely perfect the way it is!
3:30
I am watching all your videos since a random youtube recommendation. Some of the best coding videos I have seen yet, very clear, easy to follow and often about topics and features I would never thought that I will benefit from. Thanks!
Great to hear!
Best video on dataclasses I have ever seen by far. Loved the approach of firstly doiny an example implementation, then showing the issues found, and finally solving them using the feature displayed.
Awesome, thank you!
This is the type of stuff classes should teach. I really appreciate your content.
Your vids are the perfect thing for me, I usually get lost in long videos. Yours are short, simple and actually demonstrate applications of it
Great to hear!
As someone who has been slowly getting into the nitty gritty of writing classes, this video was informative, scary, and then relieving. Not only have you showed me what I have yet to learn, but also why it's something that should understood, but hopefully never written.
This is great! You do a great job of explaining these features. I always found classes to be annoying to deal with in Python and have stayed away from OOP because of that, but this approach might change my mind.
Your videos have a high learning value and I, as well as many others, appreciate your effort in making coding content. Although most of the times I do not understand what you're talking about, obviously due to me being a beginner in programming (Python), I will make sure to keep some of the things in mind and come back here much later when I'm ready.
About 2 years ago I came over from writing bare C to having Python as my daily driver. I've grown to really enjoy the language. 95% of the time it suits my needs and videos like this just illustrate how absolutely brilliant it can be. Thanks for the new trick up my sleeve!!!
This is actually one of the best CS-related channels I've watched
But its not c sharp, its python
@@aa-nw8hk CS = computer science
Thanks for putting the time and effort to explain dataclasses thoroughly. You're the best!!
Subbed to this channel at like 300 subs and now 2 weeks later you're at 12.3k. you're going places my dude, keep it up, great content
Appreciate it!
Dataclasses are a beautiful idea, and hearing "There's gotta be a better way!" reminds me of Sir Raymond's classes. Great stuff 👍!
Your videos are extremely information rich, yet not "dense" in terms of accessibility of the ideas.
**Note: As of Python 3.10 data classes DO support slots.** Thanks for adding it to the description, this video is gold!
Of course! Thanks for watching!
@@mCoding It's been 3 hours and I am still watching your videos! I was just watching another video!
👏😮 that means a lot, thanks! Glad you enjoy my videos so much!
C# dev here learning python for my Software Engineering thesis. I love your videos, directly to the point and with a lot of useful information!
I'm starting to realize how damn useful class and method decorators are. I have the cumbersome Learning Python book by Orielly but these videos offer a lot of value due to how terse they are. @cache and @dataclass all day baby
Books actually contain a lot of useful stuff! Lucky for me I love reading terse literature (high signal-to-noise-ratio).
@@mCoding Do you have some book recommendations?
@@maltml the human brain tend to work in a way - that if you didn't use it, you will forgot it....thus reading the book without goal is just a waste of time. Better to lvl the skill in ability to search for information you actually needed and when you need it. It's like - i'm watching this vid; yep cool stuff, standard lib. But if you would have a hundreds of classes, transforming each via class decorator would eat performance on module load.
don't diss lutz ;p
Great stuff man! Im new to the channel but I've already learnt a lot from your videos! Keep churning out these high quality content!! :D
Thanks! Will do!
What a nice and clear explanation! I will try to implement this in my thesis project!
Go for it!
Amazing video, love it! (loved the "hinting eye contact" @ 3:30 ;)
Wow, this is an outstanding video! Thank you so much. I just found out you’ve got a Python course, I’m in
Nice
What makes this video really useful is thay you show why you need it rather than what does it do.
Really liked this. I'm building a library for Kivy-based apps that provides an interface for working with TastyPie on a Django-based server, and this is exactly what I need for storing information related to each model schema exposed through the API. So much simpler and readable. Always good to learn more about Python, thanks for the vid!
This video gave me goosebumps... This feat is amazing!!! Great video! Very succint and useful!
Thank you so much!
This is the perfect intermediate to advanced python concepts I would like to see covered in a single comprehensive course. Your explanations are very punctual and I seem to really get a lot out of your videos. I am glad I found this channel. You should really consider, as others have suggested, to make such a course.
Noted!
This is the first video I've watched on this channel. I love this guy! Great work!
Wow, thanks!
That was soo awesome .. I saved a lot of time writing code for json responses ! Really awesome stuff 😁
wow bro this is amazing. Thanks for doing what you do man!
Wow. After this video I think I really got the gist of Python dataclasses. And so many ideas popped up in my head to refactor some code. Thanks a lot! 😃
Subscribed. Really great one. Will be following more of yours tutorials.
This is really cool and I just learned that you can put decorators on classes :) thank you!! You have earned yourself a subscriber.
Thanks for subbing!
U got me with the “I just subscribed “ look lolol
Insanely clear and valuable tutorials dude! Subbed in a millisecond
Hey, thanks!
Great video, I just found your channel and it's great, congrats
Good intermediate YT content is hard to come by, subbed! Amazing post 😁
I've just used these for my users in flask. Great explanation
I love that you called out the issue with __slots__ The default dict instance representation has turned my implentation into a massive memory hog, tuple instance representation fixed that! Maybe __slots__ and other memory optimization and profiling techniques could be the topic of a future video!
I'll cover slots eventually, but that's a more technical topic!
I think NamedTuple from typing module used slots by default. Also seem to work with @property.
I use slots to avoid typos. Particularly important if your class has writable properties.
as for now, slots are available in dataclasses too (since python 3.10)
I've being doing python for a while now and a lot of things here are new to me. Thanks for making this
Glad it was helpful!
Wow, I commented and asked for a dataclass vid a week ago and you actually delivered. Nice :)
Hope you enjoyed it!
You never fail to amuse me. Didn't know that fields thing
Thx!
One of the best Python OOP videos I've ever seen. Thanks!!
Glad it was helpful!
Higher than my current level, but when I get there, this will be even fantastic. THANK YOU !!!! :D
Very well explained. Thank you!
Very cool. I can see this saving alot of time. Python never ceases to surprise me
I know it saves me a lot of time!
When I first came across decorators and understood that the dectaror takes the function or class as an argument, I thought this will have so many uses. Data classes are gold
LMAO That pause and eye contact after he read "I just subscribed". I FELT THE PRESSURE MAN!
:D subliminal messaging
as a beginner with some basic knowledge it is the content I started peeking in, some professional stuff that I can learn actually makes life much easier!
Glad to peel back the curtain for you!
Thank you so much, I used this in one of my projects and it is really useful :)
Great to hear!
This is how a tutorial should be! Loved it 💕
I'm hooked to this course bro, a huge thanks for this.
No problem 👍
3m30... That is the new Deathstare!! Awesome, Subbed. Great vid and Mitch appreciated.
Apart from the amazing video, you're acting skills are on point :D
Love this. Real-world implementations and uses
Thanks!
That's a fantastic feature! Thanks for making this great video
And thank you for watching!
You have a very deep insight into the python source code
The new mic has arrived!
Indeed it has! Let me know if the audio quality sounds good or if I've messed something up!
@@mCoding its perfect :)
It sounds much better. Though it sounds like there's a little bit of reverb somehow
@@mCoding Your voice sounds perfect, but you can hear some vibrations from keyboard sounds in the audio
I actually left typing noises in on purpose because they sound satisfying... not sure if others agree or would rather not hear me clicking and clacking.
Wonderful video! Thanks a lot for this.
Such high quality content thanks for this, you deserve more subs
I appreciate that so much!
I especially liked it - subscribed! thanks for the tips
Awesome, thank you!
Excellent and to the point. Subscribed.
This is amazing!! Why did I learn about it so late?!?! Thx for the video
You're welcome!
WHAAAAAA THIS HELPS SOOO MUCH WITH MY AST NODES THANK YOU SO MUCH
You are very welcome 😊
Omg I just finished a Python OOP assesment at uni, where was this library! (I am also just stupidly happy I understood this video) I had all these issues so I will be using this in the future. Thanks so much, keep up the great work!
Glad I could help!
This is a very useful concept. Another great video!
Thanks again!
Great content. I use pydantic but it’s good to learn the native way for this!
Great vid! Keep up the good work!
This was so detailed - great stuff !
Glad you liked it!
This is basically like lombok's @Data and @Value for java. Nice stuff!
Really useful video and tips for Python I add no idea existed! This video reminded me of my Java college times.
Glad you liked it!
I love you, perfect video. Saved me a lot of time.
bro your channel's on fire... keep it up!!!
Appreciate it! Spread the flame!
That was a great video. Thank you❤️
Very cool. Thanks! I will use these right away. I'm thinking these will be good for the non-django parts of my app.
Have fun!
Thank You!!! Keep sharing …please. If you can and have the time, of course. 😎👏🏼👏🏼👏🏼😎
You bet!
another banger, great work mcoding, you're the best
I appreciate that!
You're ON ANOTHER LEVEL ❤️
HOLY SHIT. Why didn't I just learn this ages ago? It's fantastic!
Awesome vids. Indeed implementing this saving a lot of my time
Glad it helped!
This is excellent. Subscribed.
So basically this is python's equivalent of lombok's @Data annotation in java and python supports this out of box Thanks for sharing it Your videos are amazing. You always get to the point without wasting any time
Thanks for such kind words!
To get the nice syntax using the `attrs` library, use `auto_attribs=True` in the decorator and you'll be able to get the same syntax ability of mixed defaults in `dataclass`.
I am way too noob for this video but I'll watch it whole and watch it again after 6 months. Thanks!
such a great concept...
a perfect explanation, thank you
You make it even more complex
I haven't started learning about classes yet but I'll definitely come back to this video when I do
Ditto