Python dataclasses will save you HOURS, also featuring attrs

Sdílet
Vložit
  • čas přidán 3. 04. 2021
  • 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
  • Věda a technologie

Komentáře • 851

  • @patrickjdarrow
    @patrickjdarrow Před 3 lety +2581

    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  Před 3 lety +303

      Thanks!

    • @logankillen2669
      @logankillen2669 Před 3 lety +62

      I also think this would be cool, but would be hard not making it too long while staying informative. It seems like many CZcams 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 Před 3 lety +198

      @@logankillen2669 this isn't beginner content, it's intermediate content for programmers who want to improve.

    • @johnnybarrett3445
      @johnnybarrett3445 Před 3 lety +159

      @@johnr3936 which are the hardest tutorials to find!

    • @flowers134
      @flowers134 Před 3 lety +45

      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 Před 3 lety +1199

    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  Před 3 lety +159

      I think that's what's really the important part.

    • @AlexGb007
      @AlexGb007 Před 3 lety +16

      Totally agree! Happy CZcams recommend me your channel!

    • @parryhotter18
      @parryhotter18 Před rokem +5

      Thanks! The WHY is the part that is missing almost everywhere else. Subscribed and hoping for more :-)

    • @jeffreyhunter4115
      @jeffreyhunter4115 Před rokem +2

      Agree 100%

    • @QuintinMassey
      @QuintinMassey Před 7 měsíci

      Agreed! Context matters.

  • @octopirate-bak
    @octopirate-bak Před 2 lety +155

    Update: as of python 3.10, slots are now supported in in-built dataclasses with the ``slots`` argument

  • @dhanushkari9042
    @dhanushkari9042 Před 2 lety +58

    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  Před 2 lety +5

      Thanks so much for your kind words!

  • @zishiwu7757
    @zishiwu7757 Před 3 lety +78

    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  Před 3 lety +9

      Glad it was helpful!

  • @vnpikachu4627
    @vnpikachu4627 Před 3 lety +27

    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.

  • @nicke20686
    @nicke20686 Před 3 lety +212

    Best Python videos on youtube. Or maybe most valuable? Covering so many useful and unique topics no one else seems to.

    • @mCoding
      @mCoding  Před 3 lety +23

      Wow, thanks!

    • @gagik1
      @gagik1 Před 3 lety +3

      @@mCoding thank you very much for your videos. I know something new every video.

  • @DSCuber
    @DSCuber Před 2 lety +71

    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!

  • @usharma1624
    @usharma1624 Před 3 lety +149

    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  Před 3 lety +37

      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 Před 3 lety +24

      @@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  Před 3 lety +34

      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 Před 3 lety +4

      @@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 Před 3 lety +2

      I strongly recommend that you learn Python and OOP immediately.

  • @grahamjoss4643
    @grahamjoss4643 Před 3 lety +20

    Cool functionality. Thanks for sharing
    @ 3:32 the dead pan to the camera... Lol !
    Love the dry humor

  • @ethanperry8
    @ethanperry8 Před 3 lety +2

    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

  • @mrdbourke
    @mrdbourke Před 3 lety +20

    Wow, this is an outstanding video! Thank you so much. I just found out you’ve got a Python course, I’m in

  • @richardchin1545
    @richardchin1545 Před rokem +2

    Seen a few Python dataclasses videos but this one is the clearest and most informative in a short time. Well done!

  • @drooten
    @drooten Před 3 lety +1

    Well explained without sounding fancy. It's one of the reasons I like your videos: you know how to get to the point.

  • @d8-remy
    @d8-remy Před 3 lety +5

    This is the type of stuff classes should teach. I really appreciate your content.

  • @addvert_br0009
    @addvert_br0009 Před 3 lety +17

    This is actually one of the best CS-related channels I've watched

  • @stratfanstl
    @stratfanstl Před 2 lety +7

    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!

  • @carlesg0n
    @carlesg0n Před 3 lety +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.

  • @schneeekind
    @schneeekind Před 3 lety

    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!

  • @xavierdingeldein9336
    @xavierdingeldein9336 Před 3 lety +7

    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.

  • @mmartel
    @mmartel Před 3 lety +1

    Thanks for shining a spotlight on these very useful Python modules with a good, concise tutorial. Great comparison between attrs and dataclasses, too.

  • @kylefluto5384
    @kylefluto5384 Před 3 lety +226

    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  Před 3 lety +53

      Books actually contain a lot of useful stuff! Lucky for me I love reading terse literature (high signal-to-noise-ratio).

    • @maltml
      @maltml Před 3 lety +16

      ​@@mCoding Do you have some book recommendations?

    • @s.i.m.c.a
      @s.i.m.c.a Před 3 lety +10

      @@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 Před 2 lety

      don't diss lutz ;p

  • @ropro9817
    @ropro9817 Před 2 lety +2

    These are really great videos! I'm just learning Python for work and they're extremely helpful with simple and clear explanations.

  • @taylormallory8705
    @taylormallory8705 Před 2 lety +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!!!

  • @jorgemfgoncalves
    @jorgemfgoncalves Před 2 lety +1

    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.

  • @williambos4410
    @williambos4410 Před 3 lety +2

    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

  • @anirangoncalvesbr
    @anirangoncalvesbr Před 2 lety +1

    Dataclasses are a beautiful idea, and hearing "There's gotta be a better way!" reminds me of Sir Raymond's classes. Great stuff 👍!

  • @HubertRozmarynowski
    @HubertRozmarynowski Před rokem

    Thanks for putting the time and effort to explain dataclasses thoroughly. You're the best!!

  • @lordtejas9659
    @lordtejas9659 Před 3 lety +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  Před 3 lety +1

      Of course! Thanks for watching!

    • @lordtejas9659
      @lordtejas9659 Před 3 lety

      @@mCoding It's been 3 hours and I am still watching your videos! I was just watching another video!

    • @mCoding
      @mCoding  Před 3 lety +1

      👏😮 that means a lot, thanks! Glad you enjoy my videos so much!

  • @2fritazz
    @2fritazz Před 3 lety +11

    U got me with the “I just subscribed “ look lolol

  • @kelkka7
    @kelkka7 Před 3 lety +4

    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.

  • @bettercalldelta
    @bettercalldelta Před 3 lety

    What makes this video really useful is thay you show why you need it rather than what does it do.

  • @ChasmChaos
    @ChasmChaos Před 3 lety

    Your videos are extremely information rich, yet not "dense" in terms of accessibility of the ideas.

  • @rafael.beirigo
    @rafael.beirigo Před rokem

    Amazing video, love it! (loved the "hinting eye contact" @ 3:30 ;)

  • @ciberman
    @ciberman Před rokem +1

    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!

  • @cawinchan4282
    @cawinchan4282 Před 3 lety +1

    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

  • @bartugonulalan
    @bartugonulalan Před 2 lety +2

    LMAO That pause and eye contact after he read "I just subscribed". I FELT THE PRESSURE MAN!

    • @mCoding
      @mCoding  Před 2 lety +1

      :D subliminal messaging

  • @thiamath
    @thiamath Před 3 lety +5

    This video gave me goosebumps...
    This feat is amazing!!!
    Great video! Very succint and useful!

  • @travel.tales.official
    @travel.tales.official Před měsícem

    Subscribed. Really great one. Will be following more of yours tutorials.

  • @tiiibo
    @tiiibo Před rokem +1

    that little pause at 3:30 made me subscribe 😂

  • @mujeebishaque
    @mujeebishaque Před 2 lety

    This is the first video I've watched on this channel. I love this guy! Great work!

  • @kychemclass5850
    @kychemclass5850 Před 2 lety

    Higher than my current level, but when I get there, this will be even fantastic. THANK YOU !!!! :D

  • @johnnybarrett3445
    @johnnybarrett3445 Před 3 lety

    Insanely clear and valuable tutorials dude! Subbed in a millisecond

  • @WhyNotProgram
    @WhyNotProgram Před rokem

    Good intermediate YT content is hard to come by, subbed! Amazing post 😁

  • @jeffreyhunter4115
    @jeffreyhunter4115 Před rokem

    Very well explained. Thank you!

  • @ksz7241
    @ksz7241 Před 2 lety

    Great video, I just found your channel and it's great, congrats

  • @fahimulislam7331
    @fahimulislam7331 Před 2 lety +1

    3:31 the pause and cold look after "I just subscribed" :D

  • @thatguy6664
    @thatguy6664 Před 3 lety

    One of the best Python OOP videos I've ever seen. Thanks!!

  • @seasong7655
    @seasong7655 Před 7 měsíci

    I've just used these for my users in flask. Great explanation

  • @richardme123
    @richardme123 Před 3 lety

    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  Před 3 lety +1

      Thanks for subbing!

  • @2Sor2Fig
    @2Sor2Fig Před 3 lety

    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!

  • @JohnZakaria
    @JohnZakaria Před 3 lety +3

    You never fail to amuse me.
    Didn't know that fields thing

  • @kasta867
    @kasta867 Před 3 lety

    What a nice and clear explanation! I will try to implement this in my thesis project!

  • @raymondzhao5644
    @raymondzhao5644 Před 3 lety +1

    Wow, I commented and asked for a dataclass vid a week ago and you actually delivered. Nice :)

    • @mCoding
      @mCoding  Před 3 lety +1

      Hope you enjoyed it!

  • @sanmeetsingh4538
    @sanmeetsingh4538 Před 2 lety

    That was soo awesome .. I saved a lot of time writing code for json responses ! Really awesome stuff 😁

  • @aidanfarhi367
    @aidanfarhi367 Před 3 lety +3

    Very cool. I can see this saving alot of time. Python never ceases to surprise me

    • @mCoding
      @mCoding  Před 3 lety +1

      I know it saves me a lot of time!

  • @dgnikon
    @dgnikon Před 3 lety +13

    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  Před 3 lety +2

      I'll cover slots eventually, but that's a more technical topic!

    • @Avicenna697
      @Avicenna697 Před 3 lety

      I think NamedTuple from typing module used slots by default. Also seem to work with @property.

    • @lawrencedoliveiro9104
      @lawrencedoliveiro9104 Před 3 lety

      I use slots to avoid typos. Particularly important if your class has writable properties.

    • @omgwtfafterparty
      @omgwtfafterparty Před rokem

      as for now, slots are available in dataclasses too (since python 3.10)

  • @fartzy
    @fartzy Před 2 lety

    wow bro this is amazing. Thanks for doing what you do man!

  • @vighneshpp
    @vighneshpp Před rokem

    Wonderful video! Thanks a lot for this.

  • @amazed4778
    @amazed4778 Před rokem

    This is how a tutorial should be! Loved it 💕

  • @constantfear
    @constantfear Před 2 lety

    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

  • @paologhezzo4276
    @paologhezzo4276 Před 3 lety

    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.

  • @anonyme103
    @anonyme103 Před 2 lety

    Apart from the amazing video, you're acting skills are on point :D

  • @datenschauer
    @datenschauer Před rokem

    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! 😃

  • @fredericoamigo
    @fredericoamigo Před 2 lety

    Great vid! Keep up the good work!

  • @ShadartS
    @ShadartS Před 2 lety

    I love you, perfect video. Saved me a lot of time.

  • @Khushpich
    @Khushpich Před 3 lety

    I've being doing python for a while now and a lot of things here are new to me. Thanks for making this

  • @vaishantsah
    @vaishantsah Před 3 měsíci

    I am way too noob for this video but I'll watch it whole and watch it again after 6 months. Thanks!

  • @FarazMazhar
    @FarazMazhar Před 3 lety

    Excellent and to the point. Subscribed.

  • @roniabraham9602
    @roniabraham9602 Před 2 lety

    You have a very deep insight into the python source code

  • @cosmicpuma1409
    @cosmicpuma1409 Před 3 lety +1

    3m30... That is the new Deathstare!! Awesome, Subbed. Great vid and Mitch appreciated.

  • @DarkWizard316
    @DarkWizard316 Před rokem

    HOLY SHIT. Why didn't I just learn this ages ago? It's fantastic!

  • @poke_champ
    @poke_champ Před 3 lety

    Love this. Real-world implementations and uses

  • @daniellapm8808
    @daniellapm8808 Před 3 lety

    This is excellent. Subscribed.

  • @aqworldsmaster88
    @aqworldsmaster88 Před 3 lety

    another banger, great work mcoding, you're the best

  • @AnyFactor
    @AnyFactor Před 3 lety +164

    The new mic has arrived!

    • @mCoding
      @mCoding  Před 3 lety +32

      Indeed it has! Let me know if the audio quality sounds good or if I've messed something up!

    • @Wiedzmin940
      @Wiedzmin940 Před 3 lety +8

      @@mCoding its perfect :)

    • @deViant14
      @deViant14 Před 3 lety +5

      It sounds much better. Though it sounds like there's a little bit of reverb somehow

    • @toktik7576
      @toktik7576 Před 3 lety +2

      @@mCoding Your voice sounds perfect, but you can hear some vibrations from keyboard sounds in the audio

    • @mCoding
      @mCoding  Před 3 lety +11

      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.

  • @otesunki
    @otesunki Před 2 lety

    WHAAAAAA
    THIS HELPS SOOO MUCH WITH MY AST NODES
    THANK YOU SO MUCH

    • @mCoding
      @mCoding  Před 2 lety +1

      You are very welcome 😊

  • @dominikprager5338
    @dominikprager5338 Před 2 lety

    That 3:29 was hillariously sneaky! Good video btw

  • @dexternepo
    @dexternepo Před 3 lety

    That's a fantastic feature! Thanks for making this great video

    • @mCoding
      @mCoding  Před 3 lety +1

      And thank you for watching!

  • @samtoya
    @samtoya Před 3 lety

    I'm hooked to this course bro, a huge thanks for this.

  • @Jaun_
    @Jaun_ Před 3 lety

    This was so detailed - great stuff !

  • @vasilnikolov8576
    @vasilnikolov8576 Před 3 lety

    This is a very useful concept. Another great video!

  • @therzook
    @therzook Před 3 lety +1

    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  Před 3 lety

      Glad to peel back the curtain for you!

  • @itzblinkzy1728
    @itzblinkzy1728 Před 3 lety

    Such high quality content thanks for this, you deserve more subs

    • @mCoding
      @mCoding  Před 3 lety

      I appreciate that so much!

  • @stevecarter8810
    @stevecarter8810 Před 3 lety

    Most valuable 9 minutes of my python career

    • @mCoding
      @mCoding  Před 3 lety

      Thanks for the kind words!

  • @Moody0101
    @Moody0101 Před 3 lety

    Thank you so much, I used this in one of my projects and it is really useful :)

  • @inxiousinx7993
    @inxiousinx7993 Před 3 lety

    Awesome vids. Indeed implementing this saving a lot of my time

  • @technowey
    @technowey Před 3 lety

    Thank you for another excellent video.

  • @diegowh
    @diegowh Před 7 měsíci

    a perfect explanation, thank you

  • @tassoskat8623
    @tassoskat8623 Před 3 lety

    Great video! Rare material in CZcams. Thank you!

    • @mCoding
      @mCoding  Před 3 lety +1

      Glad you enjoyed it!

  • @geekchiq5431
    @geekchiq5431 Před 3 lety

    Thank You!!! Keep sharing …please. If you can and have the time, of course. 😎👏🏼👏🏼👏🏼😎

  • @seaniam
    @seaniam Před 3 lety

    I especially liked it - subscribed! thanks for the tips

  • @pythonista5548
    @pythonista5548 Před 7 měsíci

    You make it even more complex

  • @DrDemolition
    @DrDemolition Před 2 lety

    This channel is a goldmine.

    • @mCoding
      @mCoding  Před 2 lety +1

      Many thanks! I appreciate your kind words.

  • @behnamsalehi9765
    @behnamsalehi9765 Před 3 lety

    That was a great video. Thank you❤️

  • @simaphore
    @simaphore Před rokem

    This was an amazing video!

  • @user-vx6zk5jj4e
    @user-vx6zk5jj4e Před 3 lety

    Nicely structured video.

  • @anupamdash1102
    @anupamdash1102 Před 2 lety

    *You* my friend are the new Cory Schafer! Thank you for contributing to free education :D

  • @subhodeepsarkar7611
    @subhodeepsarkar7611 Před 3 lety

    You're ON ANOTHER LEVEL ❤️

  • @hx9d674
    @hx9d674 Před 3 lety

    This video is awesome thank you for teaching this.