If You’re Not Using Python DATA CLASSES Yet, You Should 🚀

preview_player
Показать описание

This video is a tutorial about Python's dataclasses. I take you through an example that shows what you can do with them. Python data classes are - as you'd expect - in particular suitable to model classes that represent data, and as such they offer easy mechanisms to initialize, print, order, sort, and compare data.

Note that although I'm using a sort_index attribute, strictly speaking that's not needed in this case, because a data class uses a tuple of its attributes in the class definition as the default for sorting. I'm not a fan of this kind of hidden behavior, so I prefer to do it explicitly (using something that is called sort_index in this case). Another advantage of using a separate field, is that you can do more complicated ordering, using for example a weighted combination of age and strength.

🔖 Chapters:
0:00 Intro
0:33 Behavior-driven vs data-driven classes
2:11 Explaining the example
3:31 Creating a dataclass
4:37 Sorting and comparing
6:58 Default values
7:40 Creating read-only (frozen) objects
8:55 String representation of data
9:34 Final thoughts

#arjancodes #softwaredesign #python
Рекомендации по теме
Комментарии
Автор

Just in case you're feeling low Arjan, you are doing a freaking great job here. Thank you very much ! Keep them coming :D

falak
Автор

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).

drymanic
Автор

Incredibly clear presentation. I've been programming Python for 25 years and hadn't used data classes. Thanks! Subscribed.

JohnMitchellCalif
Автор

This is a really great intro to data classes, very clear and to the point. Looking forward to more videos like this!

dennissmith
Автор

This is great! Thanks for sharing Arjan. Love your work.

CodingEntrepreneurs
Автор

Arjan, your videos are really clear and simple. You are very much appreciated. Greetings from the UK!

myce-liam
Автор

Really happy I found your channel! You and Corey Schafer are my two favourites for learning Python right now.

Keep up the excellent work!

typehint
Автор

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

skyletwings
Автор

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 YouTube, but you are the most amazing

alicebrown
Автор

I would love to see a complete python course taught by Arjan!

mithunmanoharmithun
Автор

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.

jimmy
Автор

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

DanielRios
Автор

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

cetilly
Автор

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.

theophilegaudin
Автор

I love how wisely you avoid all the issues I've had with other peoples code.

letsaram
Автор

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 !

nlight
Автор

I LOVE U, THIS IS THE FIRST STEP FOR UNDERSTAND FAST API AND PYDANTIC,
THANK U FOR URS VIDEOS

Bc-wk
Автор

Arjan keep doing your video man ! Your are a true teacher, everthing you say is just so clear. Thank you for your help :)

Yassou
Автор

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.

ldebrotb
Автор

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

martindu