This Is Why Python Data Classes Are Awesome

796,758
187
Published 2022-03-25
Data classes in Python are really powerful and not just for representing structured data. In this video, I show you what you can do with dataclasses as well as new capabilities that have been added in Python 3.10.

💡 Get the FREE 7-step guide to help you consistently design great software: arjancodes.com/designguide.

🎓 Courses:
The Software Designer Mindset: www.arjancodes.com/mindset
The Software Architect Mindset: Pre-register now! www.arjancodes.com/architect
Next Level Python: Become a Python Expert: www.arjancodes.com/next-level-python
The 30-Day Design Challenge: www.arjancodes.com/30ddc

🛒 GEAR & RECOMMENDED BOOKS: kit.co/arjancodes.

👍 If you enjoyed this content, give this video a like. If you want to watch more of my upcoming videos, consider subscribing to my channel!

💬 Discord: discord.arjan.codes/
🐦Twitter: twitter.com/arjancodes
🌍LinkedIn: www.linkedin.com/company/arjancodes
🕵Facebook: www.facebook.com/arjancodes

👀 Code reviewers:
- Yoriz
- Ryan Laursen
- Sybren A. Stüvel
- Dale Hagglund

🎥 Video edited by Mark Bacskai: www.instagram.com/bacskaimark

🔖 Chapters:
0:00 Intro
0:49 What are data classes?
1:37 Explaining the example
4:00 Dataclasses basics
5:45 Assigning default values
8:54 Excluding arguments from the initializer
9:44 Using post_init to generate extra fields
11:25 Private/protected members
11:59 Excluding information from the repr
12:43 Freezing a dataclass
14:59 (new in Python 3.10) kw_only
16:10 (new in Python 3.10) match_args
17:00 (new in Python 3.10) slots
21:30 Final thoughts

#arjancodes #softwaredesign #python

DISCLAIMER - The links in this description might be affiliate links. If you purchase a product or service through one of those links, I may receive a small commission. There is no additional charge to you. Thanks for supporting my channel so I can continue to provide you with free content

All Comments (21)
  • @saketh_s
    One thing to note, when you pass data class "frozen=True", instance is immutable. So at 13:29 you are actually getting error for search_string which we are trying to change at post_int at object creating not for person name (line 25) You can not change fields even inside the class even with setter methods.
  • @sbeau
    Arjan, your ability to explain detail in a structured, easy to follow format is second to none. Thank you for another great tutorial.
  • @coupmd
    Enjoyed this video. Would enjoy more videos on intermediate/advanced python standard library tools and their examples of their intended use cases.
  • @vedambala
    Arjan, You should seriously consider doing a full fledged Python course from beginner to advance level laden with Projects so we can learn Python the Pythonic way.
  • May you have tons of subs Arjan cause you're one of the few youtubers whose python codes aren't messed up and you're instructions are on the point and useful.
  • @wbellmangmail
    Just came across you channel. I've watched a couple of your other videos as well... I happen to be working on a project and your videos on dependency injection vs. dependency inversion as well as your video on structural pattern matching have been super helpful! Awesome stuff! Thanks for getting content out there (and chapter-izing your videos. ;) )!
  • @jordansilke3629
    This is truly an excellent successor to your earlier video on data classes, bravo!
  • @bryan_hiebert
    Just like every video from you my knowledge of programming with python is advanced. I really appreciate the content you provide, thank you!
  • @FrankenLab
    @ArjanCodes, great video. I consider myself a casual programmer and have been programming with Python for about 5yrs and absolutely love it. I started programming "casually" in the 80's with C, Pascal, Assembler, then Perl and now Python. I really enjoyed C and Perl, but Python is by far my favorite now. This is the first video I've seen of yours and based on that I just subscribed to your channel. I enjoy your style and explanations and I don't have to play the video at twice the speed waiting for you to get to the point. I'm looking forward to seeing more of your Python videos.
  • @paulorsbrito
    What happened to “explicit is better than implicit” and to “there should be only one obvious way to do something”?
  • @alfonsov3190
    I had a couple of questions during the video, but immediately after they surged, you addressed them. Thanks a lot, as usual!
  • @hematogen50g
    I came to Python from C# so I picked up coding quickly. But thinking in python way is harder, so such videos really help me develop python mindset.
  • @barrykruyssen
    Hi Arjan. Great video, very well presented. I've just found your channel and will be watching all your videos as time permits. I'm a retired software engineer (started back in the early 80's). 5 weeks ago I decided, in my retirement, to switch back to linux (and python - which I have never used), just for fun, after 25 years in microsoft products (prior to that I was in the Unix world). I'll be changing 2 of my classes to data classes ASAP. I have just read your Software Design Guide and agree with much of what you've written. I think you have glossed over the most important aspect "Who’s it intended for?". Determining who is doing what, why they do it and what they expect from it and then managing the expectations is the key to a successful implementation. You do mention your “zoom out and zoom in” approach which probably encompasses this but in my planning, the people are the key and if there are problems in this area it can be very costly down the track (we qualified our prospective clients and if the people problem was too great we walked away from deals), as you say we write the code for the client, not for us. Thanks
  • @ChrisPatti
    I really love this video! It’s an excellent quick, guided tours to how powerful data classes, can be. Thanks for making it!
  • @neoporcupine
    Working my way through my third LinkedIn Learning (LIL) Python course, all of which claim to teach you the data structures available. Arjan's short video explains a dozen reasons why you want to use dataclass that the LIL courses didn't include. Thank you!
  • @techassets
    I have an observation. Even if you remove the person.name = "Arjan" and set the dataclass frozen=True you will have a FrozenInstanceError. Because you use the _post_init_ method and the frozen dataclass does NOT allow it ! So you must remove the _post_init_ method if you want to freeze the dataclass and prove that you cannot modify it after initialization. THANK YOU FOR YOUR AMAZING VIDEOS !
  • @sambeard4428
    Geweldig en duidelijk overzicht van deze erg handige feature. Gisteren liep ik precies nog tegen deze use case aan, veel boillerplate code. Dit lost het mooi op. Thanks!
  • @mauisam1
    Again Arjan, another fantastic video. If I become half the programmer you are I will be lucky. But I just have the love of programming so I will continue to work on improving my knowledge and skill set. So many programmer say just read the Docs, but I learn from examples and the Docs usually have few if not poor examples. So a big part of my improvement will be in thanks to your love of sharing your knowledge. Thank you...
  • Great tutorial in Python, its been a while I havent done some Python, I didnt know much about DataClass. So thank you.