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

  Рет қаралды 9,903

pixegami

pixegami

Күн бұрын

Пікірлер: 21
@pixegami
@pixegami 9 ай бұрын
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 9 ай бұрын
Dude your level of detail and pace is just PERFECT! I have easily seen hundreds if not thousands of videos on KZbin 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 9 ай бұрын
Thank you so much :) I spend a lot of effort editing the script/pacing, so I really appreciate your comment.
@hugo-onzakorderra8851
@hugo-onzakorderra8851 9 ай бұрын
Nice guide to dataclasses!
@pixegami
@pixegami 9 ай бұрын
Thank you!
@ericb8494
@ericb8494 20 күн бұрын
For Java developers, it's just like records or in kotlin: dataclasses. The difference in Python is that it does not have validation and instances aren't "strictly" typed, so you can add or remove properties at will.
@ВладФоменко-р4е
@ВладФоменко-р4е 9 ай бұрын
Thank you! Great overview.
@pixegami
@pixegami 9 ай бұрын
Thanks for watching!
@FrederikSchumacher
@FrederikSchumacher 9 ай бұрын
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 9 ай бұрын
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.
@xxonoxxtg
@xxonoxxtg 9 ай бұрын
Thank you very much 🤓
@pixegami
@pixegami 9 ай бұрын
You’re welcome!
@byitkc
@byitkc 9 ай бұрын
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 9 ай бұрын
Thank you! So glad you found it helpful :)
@Orcrambo
@Orcrambo 9 ай бұрын
Nice video, +1 for Pydantic!
@pixegami
@pixegami 9 ай бұрын
Thanks!
@ubiquitousonegroup
@ubiquitousonegroup 3 ай бұрын
I have a question I would like to email it to you, I don't want to look bad in the comment section😗
@obc9794
@obc9794 9 ай бұрын
Nice introduction to dataclasses, Thanks
@pixegami
@pixegami 9 ай бұрын
Thank you!
@kpro9047
@kpro9047 2 ай бұрын
Man, I love it when I find a youtuber who is clear and thorough. You rock!!
@강동찬-h4s
@강동찬-h4s 6 ай бұрын
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.
Pydantic Tutorial • Solving Python's Biggest Problem
11:07
pixegami
Рет қаралды 274 М.
10 Important Python Concepts In 20 Minutes
18:49
Indently
Рет қаралды 139 М.
Поветкин заставил себя уважать!
01:00
МИНУС БАЛЛ
Рет қаралды 6 МЛН
OYUNCAK MİKROFON İLE TRAFİK LAMBASINI DEĞİŞTİRDİ 😱
00:17
Melih Taşçı
Рет қаралды 12 МЛН
Шок. Никокадо Авокадо похудел на 110 кг
00:44
PLEASE Use These 5 Python Decorators
20:12
Tech With Tim
Рет қаралды 113 М.
RAG + Langchain Python Project: Easy AI/Chat For Your Docs
16:42
Data Classes in Python Are The New Standard
20:34
NeuralNine
Рет қаралды 67 М.
Pydantic is OP, here's why
18:10
Carberra
Рет қаралды 23 М.
5 Tips To Write Better Python Functions
15:59
Indently
Рет қаралды 105 М.
How principled coders outperform the competition
11:11
Coderized
Рет қаралды 1,7 МЛН
The Flaws of Inheritance
10:01
CodeAesthetic
Рет қаралды 945 М.
10 Python Comprehensions You SHOULD Be Using
21:35
Tech With Tim
Рет қаралды 148 М.
5 Things I Wish I Knew Before Learning Streamlit
21:34
Fanilo Andrianasolo
Рет қаралды 30 М.