No video

Python dataclasses will save you HOURS, also featuring attrs

  Рет қаралды 885,866

mCoding

mCoding

3 жыл бұрын

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/mCo...
attrs library: www.attrs.org/...
SUPPORT ME ⭐
---------------------------------------------------
Patreon: / mcoding
Paypal: www.paypal.com...
Other donations: mcoding.io/donate
BE ACTIVE IN MY COMMUNITY 😄
---------------------------------------------------
Discord: / discord
Github: github.com/mCo...
Reddit: / mcoding
Facebook: / james.mcoding

Пікірлер: 851
@patrickjdarrow
@patrickjdarrow 3 жыл бұрын
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.
@mCoding
@mCoding 3 жыл бұрын
Thanks!
@logankillen2669
@logankillen2669 3 жыл бұрын
I also think this would be cool, but would be hard not making it too long while staying informative. It seems like many KZfaq channels favor having a 5 hour crash course. Beginners don’t have the attention span (imo) because they’ll have so many questions.
@johnr3936
@johnr3936 3 жыл бұрын
@@logankillen2669 this isn't beginner content, it's intermediate content for programmers who want to improve.
@johnnybarrett3445
@johnnybarrett3445 3 жыл бұрын
@@johnr3936 which are the hardest tutorials to find!
@flowers134
@flowers134 3 жыл бұрын
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.
@eoghainlebioda1396
@eoghainlebioda1396 3 жыл бұрын
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
@mCoding
@mCoding 3 жыл бұрын
I think that's what's really the important part.
@AlexGb007
@AlexGb007 3 жыл бұрын
Totally agree! Happy KZfaq recommend me your channel!
@parryhotter18
@parryhotter18 2 жыл бұрын
Thanks! The WHY is the part that is missing almost everywhere else. Subscribed and hoping for more :-)
@jeffreyhunter4115
@jeffreyhunter4115 Жыл бұрын
Agree 100%
@QuintinMassey
@QuintinMassey 9 ай бұрын
Agreed! Context matters.
@octopirate-bak
@octopirate-bak 2 жыл бұрын
Update: as of python 3.10, slots are now supported in in-built dataclasses with the ``slots`` argument
@msatul1305
@msatul1305 7 ай бұрын
what is that?
@dhanushkari9042
@dhanushkari9042 2 жыл бұрын
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!
@mCoding
@mCoding 2 жыл бұрын
Thanks so much for your kind words!
@zishiwu7757
@zishiwu7757 3 жыл бұрын
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.
@mCoding
@mCoding 3 жыл бұрын
Glad it was helpful!
@vnpikachu4627
@vnpikachu4627 3 жыл бұрын
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.
@samueljehanno
@samueljehanno Жыл бұрын
Yes
@nicke20686
@nicke20686 3 жыл бұрын
Best Python videos on youtube. Or maybe most valuable? Covering so many useful and unique topics no one else seems to.
@mCoding
@mCoding 3 жыл бұрын
Wow, thanks!
@gagik1
@gagik1 3 жыл бұрын
@@mCoding thank you very much for your videos. I know something new every video.
@usharma1624
@usharma1624 3 жыл бұрын
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.
@mCoding
@mCoding 3 жыл бұрын
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!
@vandelayindustries2971
@vandelayindustries2971 3 жыл бұрын
@@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.
@mCoding
@mCoding 3 жыл бұрын
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.
@usharma1624
@usharma1624 3 жыл бұрын
@@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.
@TheHippyHoppyHippo
@TheHippyHoppyHippo 3 жыл бұрын
I strongly recommend that you learn Python and OOP immediately.
@DSCuber
@DSCuber 2 жыл бұрын
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!
@grahamjoss4643
@grahamjoss4643 3 жыл бұрын
Cool functionality. Thanks for sharing @ 3:32 the dead pan to the camera... Lol ! Love the dry humor
@mCoding
@mCoding 3 жыл бұрын
I will slowly dial up the humor moving forward.
@mustafamotiwala2335
@mustafamotiwala2335 3 жыл бұрын
@@mCoding its absolutely perfect the way it is!
@toxic_narcissist
@toxic_narcissist 3 жыл бұрын
3:30
@addvert_br0009
@addvert_br0009 3 жыл бұрын
This is actually one of the best CS-related channels I've watched
@aa-nw8hk
@aa-nw8hk 3 жыл бұрын
But its not c sharp, its python
@addvert_br0009
@addvert_br0009 3 жыл бұрын
@@aa-nw8hk CS = computer science
@mrdbourke
@mrdbourke 3 жыл бұрын
Wow, this is an outstanding video! Thank you so much. I just found out you’ve got a Python course, I’m in
@samueljehanno
@samueljehanno Жыл бұрын
Nice
@stratfanstl
@stratfanstl 2 жыл бұрын
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!
@myaccount6216
@myaccount6216 2 жыл бұрын
I have no idea what's happening
@xavierdingeldein9336
@xavierdingeldein9336 3 жыл бұрын
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.
@dgnikon
@dgnikon 3 жыл бұрын
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!
@mCoding
@mCoding 3 жыл бұрын
I'll cover slots eventually, but that's a more technical topic!
@Avicenna697
@Avicenna697 3 жыл бұрын
I think NamedTuple from typing module used slots by default. Also seem to work with @property.
@lawrencedoliveiro9104
@lawrencedoliveiro9104 3 жыл бұрын
I use slots to avoid typos. Particularly important if your class has writable properties.
@omgwtfafterparty
@omgwtfafterparty 2 жыл бұрын
as for now, slots are available in dataclasses too (since python 3.10)
@carlesg0n
@carlesg0n 3 жыл бұрын
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.
@mCoding
@mCoding 3 жыл бұрын
Awesome, thank you!
@lordtejas9659
@lordtejas9659 3 жыл бұрын
**Note: As of Python 3.10 data classes DO support slots.** Thanks for adding it to the description, this video is gold!
@mCoding
@mCoding 3 жыл бұрын
Of course! Thanks for watching!
@lordtejas9659
@lordtejas9659 3 жыл бұрын
@@mCoding It's been 3 hours and I am still watching your videos! I was just watching another video!
@mCoding
@mCoding 3 жыл бұрын
👏😮 that means a lot, thanks! Glad you enjoy my videos so much!
@kylefluto5384
@kylefluto5384 3 жыл бұрын
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
@mCoding
@mCoding 3 жыл бұрын
Books actually contain a lot of useful stuff! Lucky for me I love reading terse literature (high signal-to-noise-ratio).
@maltml
@maltml 3 жыл бұрын
​@@mCoding Do you have some book recommendations?
@s.i.m.c.a
@s.i.m.c.a 3 жыл бұрын
@@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.
@narnbrez
@narnbrez 2 жыл бұрын
don't diss lutz ;p
@2fritazz
@2fritazz 3 жыл бұрын
U got me with the “I just subscribed “ look lolol
@richardchin1545
@richardchin1545 Жыл бұрын
Seen a few Python dataclasses videos but this one is the clearest and most informative in a short time. Well done!
@taylormallory8705
@taylormallory8705 2 жыл бұрын
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!!!
@AnyFactor
@AnyFactor 3 жыл бұрын
The new mic has arrived!
@mCoding
@mCoding 3 жыл бұрын
Indeed it has! Let me know if the audio quality sounds good or if I've messed something up!
@Wiedzmin940
@Wiedzmin940 3 жыл бұрын
@@mCoding its perfect :)
@deViant14
@deViant14 3 жыл бұрын
It sounds much better. Though it sounds like there's a little bit of reverb somehow
@toktik7576
@toktik7576 3 жыл бұрын
@@mCoding Your voice sounds perfect, but you can hear some vibrations from keyboard sounds in the audio
@mCoding
@mCoding 3 жыл бұрын
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.
@d8-remy
@d8-remy 3 жыл бұрын
This is the type of stuff classes should teach. I really appreciate your content.
@bartugonulalan
@bartugonulalan 2 жыл бұрын
LMAO That pause and eye contact after he read "I just subscribed". I FELT THE PRESSURE MAN!
@mCoding
@mCoding 2 жыл бұрын
:D subliminal messaging
@Julie9009
@Julie9009 3 жыл бұрын
Thank you for introducing me to dataclasses! Using a dataclass has just considerably simplified a module that I'm working on right now.
@williambos4410
@williambos4410 3 жыл бұрын
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
@mCoding
@mCoding 3 жыл бұрын
Appreciate it!
@anirangoncalvesbr
@anirangoncalvesbr 3 жыл бұрын
Dataclasses are a beautiful idea, and hearing "There's gotta be a better way!" reminds me of Sir Raymond's classes. Great stuff 👍!
@schneeekind
@schneeekind 3 жыл бұрын
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!
@mCoding
@mCoding 3 жыл бұрын
Great to hear!
@bettercalldelta
@bettercalldelta 3 жыл бұрын
What makes this video really useful is thay you show why you need it rather than what does it do.
@fahimulislam7331
@fahimulislam7331 3 жыл бұрын
3:31 the pause and cold look after "I just subscribed" :D
@mCoding
@mCoding 3 жыл бұрын
😇
@ciberman
@ciberman Жыл бұрын
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!
@thiamath
@thiamath 3 жыл бұрын
This video gave me goosebumps... This feat is amazing!!! Great video! Very succint and useful!
@mCoding
@mCoding 3 жыл бұрын
Thank you so much!
@ropro9817
@ropro9817 2 жыл бұрын
These are really great videos! I'm just learning Python for work and they're extremely helpful with simple and clear explanations.
@tiiibo
@tiiibo 2 жыл бұрын
that little pause at 3:30 made me subscribe 😂
@jorgemfgoncalves
@jorgemfgoncalves 2 жыл бұрын
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.
@kelkka7
@kelkka7 3 жыл бұрын
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.
@ChasmChaos
@ChasmChaos 3 жыл бұрын
Your videos are extremely information rich, yet not "dense" in terms of accessibility of the ideas.
@mmartel
@mmartel 3 жыл бұрын
Thanks for shining a spotlight on these very useful Python modules with a good, concise tutorial. Great comparison between attrs and dataclasses, too.
@paologhezzo4276
@paologhezzo4276 3 жыл бұрын
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.
@mCoding
@mCoding 3 жыл бұрын
Noted!
@cosmicpuma1409
@cosmicpuma1409 3 жыл бұрын
3m30... That is the new Deathstare!! Awesome, Subbed. Great vid and Mitch appreciated.
@WhyNotProgram
@WhyNotProgram Жыл бұрын
Good intermediate YT content is hard to come by, subbed! Amazing post 😁
@aidanfarhi367
@aidanfarhi367 3 жыл бұрын
Very cool. I can see this saving alot of time. Python never ceases to surprise me
@mCoding
@mCoding 3 жыл бұрын
I know it saves me a lot of time!
@raymondzhao5644
@raymondzhao5644 3 жыл бұрын
Wow, I commented and asked for a dataclass vid a week ago and you actually delivered. Nice :)
@mCoding
@mCoding 3 жыл бұрын
Hope you enjoyed it!
@ethanperry8
@ethanperry8 3 жыл бұрын
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
@mCoding
@mCoding 3 жыл бұрын
Great to hear!
@player-eric
@player-eric 2 жыл бұрын
Hi! If I want some of the attributes of an instance to be read-only, instead of all attributes frozen, could this implemented with "@dataclass"?
@rafael.beirigo
@rafael.beirigo Жыл бұрын
Amazing video, love it! (loved the "hinting eye contact" @ 3:30 ;)
@JohnZakaria
@JohnZakaria 3 жыл бұрын
You never fail to amuse me. Didn't know that fields thing
@mCoding
@mCoding 3 жыл бұрын
Thx!
@mujeebishaque
@mujeebishaque 3 жыл бұрын
This is the first video I've watched on this channel. I love this guy! Great work!
@mCoding
@mCoding 3 жыл бұрын
Wow, thanks!
@TheSkyCries1
@TheSkyCries1 3 жыл бұрын
I remember doing project for school in java where the professor didn't let you use any libraries, so if you wanted something to be comparable you had to type every thing out... my God. This brings tears to me eyes that dataclasses in pyton will automatically do all of that for you.
@ABaumstumpf
@ABaumstumpf 3 жыл бұрын
Well - the professor did the right thing - get you to actually use the language.
@HubertRozmarynowski
@HubertRozmarynowski Жыл бұрын
Thanks for putting the time and effort to explain dataclasses thoroughly. You're the best!!
@jacksonyan7346
@jacksonyan7346 3 жыл бұрын
Ey this is cool. My college classes made me design my own cpu but never actually taught me any useful stuff like this.
@SimGunther
@SimGunther 3 жыл бұрын
Both of those things are actually useful. Sucks that your college only taught you the former.
@jonasking3670
@jonasking3670 3 жыл бұрын
I shouldn’t watch this so close to going to sleep. This is going to give me weird dreams.
@mCoding
@mCoding 3 жыл бұрын
I will try my best to not appear in your dreams.
@Kenionatus
@Kenionatus 3 жыл бұрын
I heard learning before going to bed is efficient because you'll process what you learned in your sleep. Source: Idk, probably read it in the morning.
@vaishantsah
@vaishantsah 5 ай бұрын
I am way too noob for this video but I'll watch it whole and watch it again after 6 months. Thanks!
@amirh6712
@amirh6712 3 жыл бұрын
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
@mCoding
@mCoding 3 жыл бұрын
Thanks for such kind words!
@LittlePharma
@LittlePharma 3 жыл бұрын
Highly recommend checking out pydantic. It's a huge extension to normal dataclasses, adding serialisation, better validators, forced typing and more!
@mCoding
@mCoding 3 жыл бұрын
I'll check it out! Thanks for the suggestion.
@therzook
@therzook 3 жыл бұрын
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!
@mCoding
@mCoding 3 жыл бұрын
Glad to peel back the curtain for you!
@cawinchan4282
@cawinchan4282 3 жыл бұрын
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
@mCoding
@mCoding 3 жыл бұрын
Thanks! Will do!
@2Sor2Fig
@2Sor2Fig 3 жыл бұрын
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!
@DarkWizard316
@DarkWizard316 2 жыл бұрын
HOLY SHIT. Why didn't I just learn this ages ago? It's fantastic!
@thepurplesmurf
@thepurplesmurf 2 жыл бұрын
LOL that subtle hint at 3:30 😂👍
@seasong7655
@seasong7655 9 ай бұрын
I've just used these for my users in flask. Great explanation
@travel.tales.official
@travel.tales.official 3 ай бұрын
Subscribed. Really great one. Will be following more of yours tutorials.
@anonyme103
@anonyme103 2 жыл бұрын
Apart from the amazing video, you're acting skills are on point :D
@michaelmueller9635
@michaelmueller9635 Жыл бұрын
For dataclasses, there is direct support for __slots__ (added to python in 3.10). Just as an update.
@datenschauer
@datenschauer 2 жыл бұрын
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! 😃
@Khushpich
@Khushpich 3 жыл бұрын
I've being doing python for a while now and a lot of things here are new to me. Thanks for making this
@mCoding
@mCoding 3 жыл бұрын
Glad it was helpful!
@dominikprager5338
@dominikprager5338 2 жыл бұрын
That 3:29 was hillariously sneaky! Good video btw
@constantfear
@constantfear 2 жыл бұрын
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
@richardme123
@richardme123 3 жыл бұрын
This is really cool and I just learned that you can put decorators on classes :) thank you!! You have earned yourself a subscriber.
@mCoding
@mCoding 3 жыл бұрын
Thanks for subbing!
@acecabezon
@acecabezon 5 ай бұрын
I really like your style of videos and have watched most of them already. But I'll admit I'm not sold on dataclasses... With your first example, you know exactly what python will do in each use case. With dataclasses, you've wrapped your code with an opaque magic cloak that you can either simply hope that it does what you want, or you will have to test every corner of its behavior, or analyze all the corners of the dataclass code itself. And newer coders will not understand it -- I know because someone at my work uses them and even after explaining them to me I had no clue what he was talking about or what they did, and I've been writing python for 10 years.
@stevecarter8810
@stevecarter8810 3 жыл бұрын
Most valuable 9 minutes of my python career
@mCoding
@mCoding 3 жыл бұрын
Thanks for the kind words!
@icen9ne645
@icen9ne645 3 жыл бұрын
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`.
@DrDemolition
@DrDemolition 2 жыл бұрын
This channel is a goldmine.
@mCoding
@mCoding 2 жыл бұрын
Many thanks! I appreciate your kind words.
@loveUbleach4ever
@loveUbleach4ever Жыл бұрын
I took CS in bachelors, CS in masters and have PhD but never have I crossed this, thanks a lot man, this is actually pretty helpful. it shows how dumb down the CS degrees are right now, No one is doing any code and doing shit that is outdated and have no real life usage.
@thatguy6664
@thatguy6664 3 жыл бұрын
One of the best Python OOP videos I've ever seen. Thanks!!
@mCoding
@mCoding 3 жыл бұрын
Glad it was helpful!
@dogukan463
@dogukan463 3 жыл бұрын
When he read the comment "I just subscribed" and looked at the camera I immediately gave in.
@mCoding
@mCoding 3 жыл бұрын
Ty ty you've fallen for my tricks
@atakanyenelalumni27
@atakanyenelalumni27 3 жыл бұрын
I didnt know this so thanks for the video. One thing I missed was the speed comparison. You made the Manual class larger by giving the performance as an excuse(not equal and all sorting methods) but when discussing dataclass you never mentioned the performance overhead. The size of the code probably justifies it’s slower anyways but its not mentioned :(
@mCoding
@mCoding 3 жыл бұрын
Performance overhead is it probably 3x's to 10x's most operations. If you are going for pure speed, you may want to avoid wrapper classes and just use tuples. (See my video on how fast python's sort it, I compare tuple vs dataclass speed there). However, the vast majority of code is not limited by the operations you do on class objects, so for most code the impact would be minimal.
@kasta867
@kasta867 3 жыл бұрын
What a nice and clear explanation! I will try to implement this in my thesis project!
@mCoding
@mCoding 3 жыл бұрын
Go for it!
@kychemclass5850
@kychemclass5850 2 жыл бұрын
Higher than my current level, but when I get there, this will be even fantastic. THANK YOU !!!! :D
@tedarcher9120
@tedarcher9120 3 жыл бұрын
Dataclasses vs named tuples?
@mCoding
@mCoding 3 жыл бұрын
The attrs docs sums it up well. namedtuples are tuples with names, accomplished by subclassing tuple, so they are not really classes in their own right (I mean, technically they are classes, of course). namedtuples lack many type-safety features, e.g. namedtuples can compare equal to other namedtuples of different types.
@seanbrec
@seanbrec 3 жыл бұрын
If you use mypy/types a lot, I use namedtuples when I want to pass around blobs of data, instead of a list of dicts, as the type annotations on the namedtuple allows mypy to do more reasoning and catch possible errors for me then, dataclasses for items I may want to modify attributes on (namedtuples are write-only), or things that need more OOP-class-like functionality (like described in the video)
@cubbyhoo
@cubbyhoo 3 жыл бұрын
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!
@mCoding
@mCoding 3 жыл бұрын
Glad I could help!
@carelhaasbroek1575
@carelhaasbroek1575 5 ай бұрын
WHERE HAS THIS BEEN ALL MY LIFE
@joeeeee8738
@joeeeee8738 3 жыл бұрын
Great explanation. Can you explain @property? I don't think I get it right
@mCoding
@mCoding 3 жыл бұрын
If you have a zero argument function (except self) then @property allows you to call it with x.my_function instead of x.my_function(). This allows you to do things like logging, etc, but still have it look like pure attribute access to your users. This also prevents someone from calling del or assigning to the property. You can look up the "descriptor protocol" in python for the more general case if you wanted to write your own property decorator.
@KingKringel16
@KingKringel16 3 жыл бұрын
Cool and informational video! However, since I am still learning python and haven't written a lot of production code, I think it would've helped me to see more clearly where the limitations of dataclasses are. Can or should I just use them everywhere, if they are saving lots of time? When should I NOT use them?
@sanmeetsingh4538
@sanmeetsingh4538 2 жыл бұрын
That was soo awesome .. I saved a lot of time writing code for json responses ! Really awesome stuff 😁
@johnnybarrett3445
@johnnybarrett3445 3 жыл бұрын
Insanely clear and valuable tutorials dude! Subbed in a millisecond
@mCoding
@mCoding 3 жыл бұрын
Hey, thanks!
@yash1152
@yash1152 Жыл бұрын
7:33 attrs allow u to specify validators & converters wholly molly, thats awesome and exactly what i needed.
@yash1152
@yash1152 Жыл бұрын
u earned a sub - i guess this is first video of u i am actually watching youtube pushed other vids before, but never got time
@zd4w9
@zd4w9 5 ай бұрын
I'm a beginner, but I started using data classes more ad more just because it was easier to keep things organized and understandable.
@minoset6037
@minoset6037 2 жыл бұрын
wow, i thought i knew python
@luandasilva4639
@luandasilva4639 3 жыл бұрын
The *slaps table* is very important in the "There has to be a better way."
@mCoding
@mCoding 3 жыл бұрын
I'll do this in the future :)
@ksz7243
@ksz7243 2 жыл бұрын
Great video, I just found your channel and it's great, congrats
@DeepakKumar-lv4te
@DeepakKumar-lv4te 3 ай бұрын
great video. I'm not a programmer (more back end stuff ) but this was excellent to watch.
@MarkJay
@MarkJay 3 жыл бұрын
Never heard of dataclasses. They look super useful! BTW, I really like your channel.
@mCoding
@mCoding 3 жыл бұрын
Awesome, thank you!
@roniabraham9602
@roniabraham9602 2 жыл бұрын
You have a very deep insight into the python source code
@Chrisdashes
@Chrisdashes 2 жыл бұрын
The fact that I was lost 10 seconds in is a testament, I need to get back to this. I'mma just save this to the list. (30-40 hrs into learning python)
@siberx4
@siberx4 3 жыл бұрын
If your use-case requires more capability than dataclasses can provide, I highly recommend looking into pydantic instead of attrs. We switched from attrs to pydantic in a project of ours that makes heavy use of these dataclass-like patterns for things like packet handling, and pydantic has been a huge step up from attrs. It has much more powerful validation mechanisms, cleaner and less quirky syntax, integrates very nicely with typing and provides a lot of useful mechanisms for generating objects from data you receive remotely (say as JSON) rather than creating them yourself, or that you need to transmit to other external services/components. It even has built-in support for automatically generating JSON schemas from the pydantic classes, which can make it much easier to provide a machine API for interfacing with your classes or generate a GUI using something like json-editor.
@alvincruz8296
@alvincruz8296 2 жыл бұрын
My search is over, @dataclass put me in a whole new level.
@FR33Willi
@FR33Willi 2 жыл бұрын
you just explained private vs public attributes in like 5 seconds, something that i didnt understand after years of coding lol
@user-rm5bq6ih6s
@user-rm5bq6ih6s 3 жыл бұрын
And on that moment when he said "put that code and delete it" i start laughing and go for a break to make a cup of tea
This Is Why Python Data Classes Are Awesome
22:19
ArjanCodes
Рет қаралды 800 М.
25 nooby Python habits you need to ditch
9:12
mCoding
Рет қаралды 1,7 МЛН
Я не голоден
01:00
К-Media
Рет қаралды 10 МЛН
Look at two different videos 😁 @karina-kola
00:11
Andrey Grechka
Рет қаралды 8 МЛН
Советы на всё лето 4 @postworkllc
00:23
История одного вокалиста
Рет қаралды 5 МЛН
Python Data Classes Are AMAZING! Here's Why
16:11
Tech With Tim
Рет қаралды 76 М.
The Fastest Prime Number Algorithm
5:23
MarshySyntax
Рет қаралды 2,3 М.
Does this sound illusion fool you?
24:55
Veritasium
Рет қаралды 804 М.
How I Would Learn Python FAST in 2024 (if I could start over)
12:19
Thu Vu data analytics
Рет қаралды 194 М.
How Senior Programmers ACTUALLY Write Code
13:37
Thriving Technologist
Рет қаралды 1,5 МЛН
Unlocking your CPU cores in Python (multiprocessing)
12:16
mCoding
Рет қаралды 300 М.
How To Use: "@dataclass" In Python (Tutorial 2023)
15:01
Indently
Рет қаралды 26 М.
20 Programming Projects That Will Make You A God At Coding
14:27
The Coding Sloth
Рет қаралды 864 М.
Pydantic Tutorial • Solving Python's Biggest Problem
11:07
pixegami
Рет қаралды 260 М.