Does Pydantic Replace Dataclasses in Python?
Вставка
- Опубліковано 19 чер 2024
- Today we learn about Pydantic and discuss whether it replaces Python's dataclasses or not.
◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾
📚 Programming Books & Merch 📚
🐍 The Python Bible Book: www.neuralnine.com/books/
💻 The Algorithm Bible Book: www.neuralnine.com/books/
👕 Programming Merch: www.neuralnine.com/shop
🌐 Social Media & Contact 🌐
📱 Website: www.neuralnine.com/
📷 Instagram: / neuralnine
🐦 Twitter: / neuralnine
🤵 LinkedIn: / neuralnine
📁 GitHub: github.com/NeuralNine
🎙 Discord: / discord
🎵 Outro Music From: www.bensound.com/
Timestamps:
(0:00) Intro
(0:20) Pydantic Fundamentals
(3:16) Validating Fields
(9:43) Root Validators
(12:55) Parsing JSON Data
(16:38) Outro - Наука та технологія
The nested if's can easily be turned into a sequence of guard clauses, which should ease the burden on the eyes and boost the understanding of what the code is supposed to do. Just a hint... :)
No, it doesn't! They are completely different things. Pydantic is a validation and serialization library, dataclasses is essentially a code generation tool for classes, which help you avoid boilerplate when defining classes. They have totally different features, and are completely different things!
If you would use pydantic instead of dataclasses, your code would be way slower. You can't use dataclasses instead of pydantic, because it doesn't provide serialization and you can't really validate user input with it.
That makes sense. not everything needs validation. If you work with the data you most certainly should consider pydantic. on that note. is the code slower because it has to do serialization?
Amazing and clear explanation as usual! However, one criticism I have is regarding the speed of delivery of information. Nonetheless, we appreciate your abundant knowledge
thanks this helped me out I really need to start using this!
Great explanations! Thanks! Your videos are very useful
Hey can we put constructor inside? Tried it but when I do the validators are not invoked.
can we have more advance video on this ? bdw Kudos for this !!😉
Great breakdown
Thanks you very much 👍
i have a doubt, how to destructor the JSON file, if we have nested objects inside an array ?
So are you the guy that writes the chatgpt produced codes , because you are amazing. Dammn
Great explanations! Thanks!
Hello, can someone explain to me, why there is a @classmethod before defining a validator? I think it's optional, because the documentation on pydantic does not mention that. I'm just curious why it is there
It is a decorator from Core-Python to indicate that a method belongs to the class and not to a specific instance. We also have the cls parameter instead of the self parameter.
@@NeuralNine Thanks for the answer :)
@@NeuralNine Isn't it the case that Python 3.11 is going to remove chaining decorators? I checked the pydantic documentation just now and @pucek365 seems to be right. There is no classmethod decorator but they use cls if they decorate with validator. They probably changed it because of forward compatibility.
could you post the git repo?
excelente explicación
What could we do if someone makes an assigment to an attribute after the instance? As far as i understand, custom validations are not executed when that happens.
well, that's the role of a property setter. You can also patch the __setattr__ of your base model to achieve that
for root_validators
the "values" is a pydantic keyword? how does it know that "email" is one of the fields?
more dataclasses with functions inside the dataclass!
Any way to sort the data?
really good
Not sure what I am doing wrong but the code does not work for me. Using Python 3.10.7 64-bit
Thx.
CONGRATULATIONS
I cried when i saw this nasted if's in 7:30