Do We Still Need Dataclasses? // PYDANTIC Tutorial

preview_player
Показать описание
Pydantic is a very useful package that makes dealing with data much easier, similar to what Python's built-in dataclasses do. Where Pydantic shines is in dealing with validating and sanitizing data. In this tutorial video, I show you how it works, and when to choose Pydantic over dataclasses in your code projects.

🎓 Courses:

A few interesting links to articles and books:

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

👀 Channel code reviewer board:
- 0xUnicorn
- Ryan Laursen
- Sybren A. Stüvel

🔖 Chapters:
0:00 Intro
0:52 Explaining the example
1:54 Creating a Pydantic Basemodel
4:42 Adding field validation
9:47 Adding model-level validation
12:54 More possibilities by adding a Config class
14:03 Converting the model instances to a dictionary
14:38 Other things that Pydantic can do
15:38 So, are dataclasses obsolete?

#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 each week!
Рекомендации по теме
Комментарии
Автор

In YouTube there are lot of python contents. But 99% are for beginners. You are the only guy who teach python advance concepts very clearly. You are doing a such a amazing job sir. Keep it up. Good Luck! 🙌

anuruddhabandara
Автор

I was just looking up fast api, with tortoise and this. You are literally reading my brain like an open book. Thanks for uploading as always.

Newascap
Автор

Thank you very much for choosing topics that are not only for beginners but for more advanced users. This kind of topics are really enjoyable.

raccoonteachesyou
Автор

After watching and rewatching your content for just 2 weeks I can already let you know that you have made me a better Software -Engineer.
Your creative work matters to many!
Thank you a lot!

Lucax
Автор

In my opinion Pydantic serves a very specific purpose, which is parsing and validating data. If you know what data you are working with and are sure what type of data you have, there is just no reason to use pydantic as the instance creation time is higher for a pydantic model compared to a dataclass

mei
Автор

3:21 Python 3.9 deprecated typing.List in favor of built-in type list (or dict, set. tuple, etc), which is much nicer because you don't need to import anything. (Additionally, there were many other deprecations from the typing module in favor of their collections.abc counterparts, so make sure to check that out too). Really these days, I only go to typing for "type theory" kind of stuff, like Any and Optional. Great video nonetheless! I'm a big user of attrs, which fills a a similar, but different, void, and didn't really know how they compared until I watched this video. Thanks!

tmmrtn
Автор

Great video. Things to note about the difference vs dataclasses. By doing runtime (i.e. on every object of your class creation and every change to validated field) you are taking quite a considerable performance hit. So don't use pydantic on classes you don't need to. If your data comes from the outside (user input, file, HTTP request and so on) you should absolutely validate it but don't do that for data-focused class. Also pydantic offers a module called `pydantic.dataclasses` that lets you use dataclasses with validation. Note that BaseModel provides another functionality as well but if you don't need it and want some validation for your datalcasses you can use this with minial changes to your dataclass-using code.

NoxMarPersonal
Автор

Oh Man, I'm from Brazil and for a long time I've been looking for this kind of content. With this whole technology bubble I've just seen superficial content about technology "how to create a crud in Django" and it has its own value but when you decide to go deep into the technology that you use most part of time we needed to get a book and try to extract as much as possible from it, but now we have you buddy, it's so nice to hear this deep content about python and it's like you've said "if you want to become a better software developer" you should probably go with arjan codes hahaha. Thank you for your content.

luizhenriquelongo
Автор

I really needed your videos to get on next level no other channel covers what your channel covers.
thankyou so much

amitkumarSaveSoil
Автор

One of my favourite things with Pydantic is that it supports nested objects i.e an object as an attribute for another object. Really helpfull when dealing with related objects.

pinakadhara
Автор

Really helpful video! Even just finding out dataclass is the better fit for my project and seeing some good clean code is always a chance to learn. I really like that you put emphasis on the small things of nice code like adding docstrings.

Do you have a video on what class methods are?

Sycord
Автор

videos are getting better and better. Thanks a lot for your time and effort, much appreciated.

CharanThakur
Автор

Thank you so much for this very interesting Pydantic example with both validation levels Arjan! 🙏😀
I'm hoping you'll blend in more Pydantic in your tutorials in the future.
FYI: for the price field you can use Decimal or condecimal with max_digits and decimal_places to more accurately represent money.

silkogelman
Автор

wow, i was just searching for a tutorial on pydantic. As a newbie, i can say that your videos have single handedly upgraded my knowledge of the field. thank you very much and please keep up the good work 😊

zarakolagar
Автор

your content is so good, i learn a lot and it made me better.

Thank you a lot!

Julien-hgjh
Автор

Wow! Beautiful coding... And Pydantic, very powerful! love that dict(exclude/include feature), remembers me a NoSql DB style query.

stifferdoroskevich
Автор

Really awesome video, I feel like I'm learning a lot about best practices really quickly watching these.

maxwell
Автор

Just fell in love with pydantic. I was poking around the code and noticed that pydantic solves the problem for nested dataclasses, for this matter I modified the title and subtitle to support mult lang (each field was now a list of text and lang pair), i created an extra class called MultLangStr and modified the json, pydantic identified the nested information and used the proper class whilst built-in dataclass ignored it and keept the dict format.

LucasNaruto
Автор

Excellent tutorial on pydantic ! :) Thank you! :) 🙌

sukanyar
Автор

Great introduction. I didn't know about Pydantic before but now introduced this in one of my own open source projects. You have a new subscriber. ❤️

evyli
join shbcf.ru