Python Dataclasses: Here's 7 Ways It Will Improve Your Code

  Рет қаралды 8,076

pixegami

pixegami

Күн бұрын

Dataclasses in Python simplify the creation of data structures with minimal code, and gives you a lot of useful utility right out of the box.
🔗 docs.python.org/3/library/dat...
📚 Chapters
00:00 Introduction
00:25 What is a Dataclass?
01:06 Create Data Structures with Less Code
02:34 Descriptive REPR
03:30 Built-in Equality Check
04:11 Fields and Default Values
06:19 Frozen Objects
07:16 Ordered Objects
08:21 Convert To and From Dict
#pixegami #python

Пікірлер: 19
@pixegami
@pixegami 7 ай бұрын
Here's some additional important details about @datacalsses that a viewer called out (thank you @FrederikSchumacher): 1. Type annotations are not validated or enforced - they can act as a nice guide for you, but be aware that Python won't throw and error or warn you if you use a different type in runtime. 2. Frozen (constant) lists can have items appended/removed (similar to a constant array in Typescript). If you want the list operations itself to be immutable too, try using tuples instead. 3. **kwargs initialisation works on the initial instance of the dataclass, but not on nested dataclasses or fields (there you might want to look at Pydantic instead, which I believe does do that).
@Aceptron
@Aceptron 7 ай бұрын
Dude your level of detail and pace is just PERFECT! I have easily seen hundreds if not thousands of videos on KZfaq regarding CS. You're one of the best on that aspect! And I love how you leave further details for documentation, which is necessary anyways!
@pixegami
@pixegami 7 ай бұрын
Thank you so much :) I spend a lot of effort editing the script/pacing, so I really appreciate your comment.
@byitkc
@byitkc 7 ай бұрын
Great introduction to dataclasses! I especially appreciated the tip about making dataclasses immutable, something I will definitely use on my future projects. Thank you!
@pixegami
@pixegami 7 ай бұрын
Thank you! So glad you found it helpful :)
@FrederikSchumacher
@FrederikSchumacher 7 ай бұрын
Really solid video. I'm really glad the options frozen and order where presented. I wish you had mentioned something potentially confusing about the type annotations: although the type annotations are required for the fields declaration, they're not validated or enforced. Meaning it's possible to declare a field "name: str" but then assign some other type of value. Another gotcha is even when using frozen, this only applies to assignment to the fields, not the values. So something like "tags: list = field(default_factory=list)" still allows modification of the list (eg. using append or extend). And one more gotcha about the **kwargs initialization: this only applies to the initial instance, not any nested fields using dataclasses - that's related to the aforementioned field types not being "used" internally. This can become potentially confusing and lead to bugs, if one isn't aware of that. So, while dataclasses.asdict works recursive, there's not really an equivalent fromdict and using **kwargs can introduce bugs.
@pixegami
@pixegami 7 ай бұрын
Thanks for your detailed comments and elaboration on the dataclasses :) I've added a pinned comment to the video with those details so everyone can read them too. You're absolutely right, good points to be aware of for anyone using dataclasses.
@user-ky1fv4ul2c
@user-ky1fv4ul2c 7 ай бұрын
Thank you! Great overview.
@pixegami
@pixegami 7 ай бұрын
Thanks for watching!
@Orcrambo
@Orcrambo 7 ай бұрын
Nice video, +1 for Pydantic!
@pixegami
@pixegami 7 ай бұрын
Thanks!
@obc9794
@obc9794 7 ай бұрын
Nice introduction to dataclasses, Thanks
@pixegami
@pixegami 7 ай бұрын
Thank you!
@xxonoxxtg
@xxonoxxtg 7 ай бұрын
Thank you very much 🤓
@pixegami
@pixegami 7 ай бұрын
You’re welcome!
@hugo-onzakorderra8851
@hugo-onzakorderra8851 7 ай бұрын
Nice guide to dataclasses!
@pixegami
@pixegami 7 ай бұрын
Thank you!
@user-rb8oi5gj6c
@user-rb8oi5gj6c 4 ай бұрын
The dataclass decorator provides useful features, but it is often used even when it doesn't fit the name dataclass, which can decrease code readability. In fact, it is confusing to see the dataclass decorator attached to classes that are only used once. The simplification of __init__ and __repr__ should be separated and implemented under different names.
@ubiquitousonegroup
@ubiquitousonegroup Ай бұрын
I have a question I would like to email it to you, I don't want to look bad in the comment section😗
Pydantic Tutorial • Solving Python's Biggest Problem
11:07
pixegami
Рет қаралды 253 М.
PLEASE Use These 5 Python Decorators
20:12
Tech With Tim
Рет қаралды 103 М.
Little girl's dream of a giant teddy bear is about to come true #shorts
00:32
Clown takes blame for missing candy 🍬🤣 #shorts
00:49
Yoeslan
Рет қаралды 40 МЛН
Опасность фирменной зарядки Apple
00:57
SuperCrastan
Рет қаралды 6 МЛН
Mama vs Son vs Daddy 😭🤣
00:13
DADDYSON SHOW
Рет қаралды 28 МЛН
5 Good Python Habits
17:35
Indently
Рет қаралды 451 М.
Streamlit: The Fastest Way To Build Python Apps?
11:57
pixegami
Рет қаралды 85 М.
Python Data Classes Are AMAZING! Here's Why
16:11
Tech With Tim
Рет қаралды 74 М.
5 Useful Python Decorators (ft. Carberra)
14:34
Indently
Рет қаралды 93 М.
15 Python Libraries You Should Know About
14:54
ArjanCodes
Рет қаралды 376 М.
Attrs, Pydantic, or Python Data Classes?
17:10
ArjanCodes
Рет қаралды 81 М.
How To Use: "@dataclass" In Python (Tutorial 2023)
15:01
Indently
Рет қаралды 26 М.
5 Useful Dunder Methods In Python
16:10
Indently
Рет қаралды 55 М.
Modern Python logging
21:32
mCoding
Рет қаралды 165 М.
Pydantic is the Python Package I Wish I'd Learned Earlier
11:21
John Watson Rooney
Рет қаралды 53 М.
Little girl's dream of a giant teddy bear is about to come true #shorts
00:32