No video

Attrs, Pydantic, or Python Data Classes?

  Рет қаралды 82,650

ArjanCodes

ArjanCodes

Күн бұрын

Data classes are a valuable tool in the Python programmer's toolkit. Despite their usefulness (I do like them a lot!), they do have limitations. In this video, I compare dataclasses with two alternative options, attrs and Pydantic, to help you decide which one to use in your code.
GitHub repository here: github.com/Arj...
✍🏻 Take a quiz on this topic: www.learntail....
🚀 Next-Level Python Skillshare Class: skl.sh/3ZQkUEN
💡 Get my FREE 7-step guide to help you consistently design great software: arjancodes.com....
💻 ArjanCodes Blog: www.arjancodes...
🎓 Courses:
The Software Designer Mindset: www.arjancodes...
The Software Designer Mindset Team Packages: www.arjancodes...
The Software Architect Mindset: Pre-register now! www.arjancodes...
Next Level Python: Become a Python Expert: www.arjancodes...
The 30-Day Design Challenge: www.arjancodes...
🛒 GEAR & RECOMMENDED BOOKS: kit.co/arjancodes.
👍 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!
💬 Discord: discord.arjan....
🐦Twitter: / arjancodes
🌍LinkedIn: / arjancodes
🕵Facebook: / arjancodes
📱Instagram: / arjancodes
👀 Code reviewers:
- Yoriz
- Ryan Laursen
- James Dooley
- Dale Hagglund
🎥 Video edited by Mark Bacskai: / bacskaimark
💻 Code example by Henrique Branco: / henriqueajnb
🔖 Chapters:
0:00 Intro
1:34 Example explanation
4:20 Comparison
7:07 Attribute validation
11:26 Pros and cons dataclasses
13:00 Pros and cons attrs
14:14 Pros and cons Pydantic
15:58 Final thoughts
16:42 Outro
#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!

Пікірлер: 124
@ArjanCodes
@ArjanCodes 10 ай бұрын
💡 Get my FREE 7-step guide to help you consistently design great software: arjancodes.com/designguide.
@BPopes
@BPopes Жыл бұрын
Answering your question in the video about Pydantic validation (~ 14:53 ), pydantic's default mode is to validate on instantiation only. But you can set validate_assignment=True in the ConfigModel of your model to validate when you assign as well.
@drheaddamage
@drheaddamage Жыл бұрын
I don't get the purpose of this. When would you want to validate on instantiation, but not on assignment? Sounds like a good way to complicate your debugging if you assume a variable is a particular type and then turns out to be overwritten with something completely different.
@matthiashomann6381
@matthiashomann6381 Жыл бұрын
@@drheaddamage I guess the use case for only validating at instantiation is that after that you may trust it based on the code and not validating later may provide performance benefits. The instantiation could be with external data (user input, config file, API data) while the subsequent assignments are within the code itself (e.g. calculations)
@ripichipina
@ripichipina Жыл бұрын
Also validation on every assignment is quite expensive operation for pydantic. There is a pattern when you create only unchangeable instances. Like using dataclasses ‘frozen’ init parameter. And if you want to change instance, you should create a new one.
@Nobody313
@Nobody313 Жыл бұрын
@@drheaddamage Validation on assigment could be very expensive. If you use a functional approach, you will never modify data (objects) just create new ones, so makes sense.
@piotrgretkierewicz3906
@piotrgretkierewicz3906 Жыл бұрын
@@drheaddamage It is not needed with frozen classes and those are extensively used in some projects. I think there was some argument that this is a bit more secure way of keeping data - creating new objects over editing existing one
@joaopedrorocha5693
@joaopedrorocha5693 Жыл бұрын
Great video! The idea is great ... comparing tools and them giving your nuggets of wisdom on which one to choose for each job ... It's great to have a glimpse of your experience while choosing tools. Suggestion for a next one: comparison between web frameworks (such as fast api, django, flask)
@yyfyyf6829
@yyfyyf6829 Жыл бұрын
Like to see that as well!
@dorb1337
@dorb1337 Жыл бұрын
Great idea
@timorieber
@timorieber Жыл бұрын
Hi Arjan, thanks for your effort you put in this videos, I got a lot of inspiration from them in the last years! I wanted to add, that Pydantic also has a dataclass decorator which is a drop-in replacement for the standard dataclasses, with all the validation features available as for the Pydantic BaseModel. Perhaps that would have been the more comparable choice. Keep up the great work! Timo
@evandrofilipe1526
@evandrofilipe1526 Жыл бұрын
It has all the features of pydantic without inheritance?!
@timorieber
@timorieber Жыл бұрын
@@evandrofilipe1526 it has all the validation features, but it is not a replacement for the Pydantic BaseModel. I recommend their docs for further details.
@matthiashomann6381
@matthiashomann6381 Жыл бұрын
One thing to note is that the Pydantic dataclass has some restrictions compared to the Pydantic BaseModel features. Would be nice to compare the Python built-in dataclass, the Pydantic dataclass and the traditional Pydantic BaseModel.
@romanzaiev
@romanzaiev Жыл бұрын
Thanks for the video! We use both dataclasses and pydantic in our projects for 2 different cases. Pydantic is more than a schema validation library today but we consider it should be used as a schema validation tool only. So, we use it extensively for the request/response schemas and some other things. On the other hand we have data models that we map on the corresponding DB tables and we use dataclasses for that. Basically, the standard flow is request -> pydantic validation -> some logic and dataclass models -> pydantic validation -> response. Sometimes it may be handy to map the schema to the model data automagically and for that case there is an orm_mode flag in pydantic. I just want to say they are not competitors at all.
@horoshuhin
@horoshuhin Жыл бұрын
Arjan it would be awesome if you'd interview the creator of pydantic. It would be a fantastic episode of "Become A Better Software Developer" or something like that. I don't know if you've thought about this kind of format. I'd love to see pros dive into the nitty gritty and share ideas or the way to do stuff. Just a thought.
@ArjanCodes
@ArjanCodes Жыл бұрын
Thanks for the suggestion!
@guyonlead
@guyonlead Жыл бұрын
I’m been rocking Pydantic for about a month now and I’m absolutely in love with it. It’s simpler to use than data classes IMO when using REST API. Granted I have a bunch of base models into another class then I use my own methods to manipulate the data but it works out nicely in the end.
@dpav02
@dpav02 Жыл бұрын
Agreed. Pydantic also works extremely well when using an ORM like Sqlalchemy.
@rx97_mc
@rx97_mc Жыл бұрын
You've been releasing banger videos one after another, i swear, I link or get linked your video hours after release!
@osjerick
@osjerick Жыл бұрын
Awesome video! I use dataclasses and Pydantic a lot. I'll take a look at attrs now. Adding Mypy to my dev workflow has also helped me deliver better code.
@grzegorzbajson3897
@grzegorzbajson3897 Жыл бұрын
It's worth to mention the performance. Dataclasses are way faster than BaseModel (I learnt this the hard way :-/). There are some improvements expected with Pydantic 2.0, but for now: In [1]: from dataclasses import dataclass In [2]: @dataclass ...: class A: ...: a: int = 1 ...: b: str = "one" ...: In [3]: %timeit a = A() 80.5 ns ± 0.212 ns per loop (mean ± std. dev. of 7 runs, 10,000,000 loops each) In [5]: from pydantic import BaseModel In [6]: class B(BaseModel): ...: a: int = 1 ...: b: str = "one" ...: In [7]: %timeit b = B() 1.03 µs ± 3.7 ns per loop (mean ± std. dev. of 7 runs, 1,000,000 loops each)
@KoyaMel
@KoyaMel Жыл бұрын
literally 5 minutes ago, I was in the situation to decide which to use. (you read my mind)
@ArjanCodes
@ArjanCodes Жыл бұрын
Haha, so I was just in time :).
@daverichardhadley
@daverichardhadley Жыл бұрын
What did you choose?
@TheSzybas
@TheSzybas Жыл бұрын
@@ArjanCodes because the wizard is not late nor early. Just in time. Like a compiler...
@KoyaMel
@KoyaMel Жыл бұрын
dataclass for the win
@dorb1337
@dorb1337 Жыл бұрын
Great video ! a question that pops - Where are you getting your knowledge from ? reading the documentation is nice, and playing around with a module or a library is great - but aren't you afraid to miss out few of the features ? are there any good forums or knowledge resources you're using to stay up to date and understand all the possible features of a library or module you use ?
@simmonslucas
@simmonslucas Жыл бұрын
You already convinced me of dataclasses. They spread across our code base like a slime mold.
@ArjanCodes
@ArjanCodes Жыл бұрын
Haha, good to hear 😊
@shS83
@shS83 5 ай бұрын
Great video. Good explanation for using int in price. This, like your other videos, come in handy in lots of different scenarios. Thank you for your contribution.
@ArjanCodes
@ArjanCodes 4 ай бұрын
I'm happy to hear you're enjoying the content!
@NopeNotMeNoSiree
@NopeNotMeNoSiree 9 ай бұрын
I had chosen pydantic for a few projects, but now I'm ripping it all out because of the incompatible API changes brought by 2.0. It appears the authors of pydantic are _very_ opinionated about how method names should be formatted, so they cavalierly replaced parse_raw by model_validate_json. Not only is the function name much longer, but it apparently does exactly the same thing. If I keep pydantic in the project, then it's like a ticking time bomb for future developers.
@cppsage
@cppsage 4 ай бұрын
Hehe, this is REALLY a NIT. I was told that zero is neither negative or positive, that function in the attr example is non-negative, not positive. (Of course with IEEE-754 we can get zero BOTH negative or positive, but that is only useful for a zero that isn’t really zero, just too small)
@domenechj
@domenechj Жыл бұрын
Great video as always! By the way, what library do you prefer for performing validation on dataframes?
@babc142
@babc142 Жыл бұрын
I've used pandera pretty heavily in production and it's very capable and the developer is super helpful. But I haven't explored pydantic extensively, and I feel there might be some advantages to using it in exchange for writing quite a lot more code
@cppsage
@cppsage 4 ай бұрын
Can’t read all comments, but I think I saw a bug in the dataclass example. I think if your code would ever run through a midnight you’d need that default for the order date to also be dynamic with default_factory, otherwise it’ll be the date of the creation of the *class* and not the instance. So if I started my program December 2023, all my orders will come up with that date by default.
@tjanos88
@tjanos88 Жыл бұрын
Thank you, I learned a lot again. Have a nice weekend!
@cppsage
@cppsage 4 ай бұрын
And another nit in the attrs example, and this may very well be my misunderstanding of str, but instead of lower should rather we use casefold for comparisons? As I understand that is dedicated for that purpose.
@viajeradelaluz
@viajeradelaluz Жыл бұрын
Great Video! I actually started using dataclasses when I saw your video about it few months ago. I 'd like you to make a short video with pros and cons about your recommendation to use int() instead of float() type for prices fields. You left me thinking about that idea. Many thanks! 😋
@ArjanCodes
@ArjanCodes Жыл бұрын
Great suggestion!
@GeorgelPreput
@GeorgelPreput Жыл бұрын
Would be great to have a follow-up which compares validation in pydantic, param and traitlets
@ArjanCodes
@ArjanCodes Жыл бұрын
Noted!
@hexomega9445
@hexomega9445 Жыл бұрын
Thanks so much to bring this to the table!
@pamdemonia
@pamdemonia Жыл бұрын
Just a late thank you for your dataclasses video which taught me about the __post_init__() method which really came in handy recently.
@CarlosMorenoV
@CarlosMorenoV Жыл бұрын
Nice video, I realised about attrs because of Fluent Python 2nd edition by Luciano Ramalho. There is an entire section in that book about Data classes. If you ask me, I prefer to use attrs or Pydantic. You can use dataclass to prototype an initial version of some tiny app. However, once you need to build something more professional, you definitely need to go beyond. And Attrs or Pydantic is the correct choice.
@potens1
@potens1 Жыл бұрын
About the representation in pydantic, instead of printing the banana, you can do print(repr(banana)), you will get Banana(name='banana', category='fruit'....).
@TomTrval
@TomTrval Жыл бұрын
Great overview thank you :) Personal preference: dataclass with `slots=True`, I hope we get build in object pooling like `pool=300` one day for for dataclasses
@GigaMarou
@GigaMarou Жыл бұрын
Hey Arjan, great video. How would you use dataclasses, attrs or pydantic to validate Tabular Machine Learning data. Greetings from Germany :)
@mithunmanoharmithun
@mithunmanoharmithun Жыл бұрын
Awesome content as always! Could you please make video on how to persist these objects into a datbase and best practices to do so ?
@ArjanCodes
@ArjanCodes Жыл бұрын
Thanks! Your suggestion is noted :)
@mariamozgunova9584
@mariamozgunova9584 Жыл бұрын
In Python, one can also use `Decimal` to represent monetary amounts
@MedievalChips
@MedievalChips Жыл бұрын
Thank you for the great video. Love the deep dive in new packages.
@ArjanCodes
@ArjanCodes Жыл бұрын
Glad you enjoyed it!
Жыл бұрын
As always, awesome video! Thank you for the great content. I'm improving a lot my developer skills with your lessons.
@ArjanCodes
@ArjanCodes Жыл бұрын
Happy to help!
@helish_88
@helish_88 Жыл бұрын
My english is not so good, usually I don't understand all but ur speech is so good, I understood almost everything, btw its very helpful video
@lawrencedoliveiro9104
@lawrencedoliveiro9104 Жыл бұрын
4:07 Why not use the Decimal type?
@MrVernuk
@MrVernuk 5 ай бұрын
Thank you fot this video! It's really helpfull ☺
@ArjanCodes
@ArjanCodes 4 ай бұрын
I'm glad to hear the video was helpful!
@Patrick-hl1wp
@Patrick-hl1wp Жыл бұрын
Super helpful information, thanks Arjan
@ArjanCodes
@ArjanCodes Жыл бұрын
Glad it was helpful!
@hargovind2776
@hargovind2776 Жыл бұрын
I hit like even before starting your video. Already know the content's gonna be amazing, keep doing the awesome work!!!!
@ArjanCodes
@ArjanCodes Жыл бұрын
Thank you so much!
@lawrencedoliveiro9104
@lawrencedoliveiro9104 Жыл бұрын
4:50 Sounds like defining key fields in a database record.
@PavithraSuLa
@PavithraSuLa Жыл бұрын
Awesome video! Thank you for the great content. Could you please make video about python metaprogramming and metaclass with real world example.
@ArjanCodes
@ArjanCodes Жыл бұрын
Thanks for the suggestion it's noted!
@lawrencedoliveiro9104
@lawrencedoliveiro9104 Жыл бұрын
7:38 As soon as you said it uses subclassing, I immediately thought “there must be a metaclass involved”. Had a look at the source code, and yes, that is how it works.
@danimperiale8545
@danimperiale8545 Жыл бұрын
Dude, great video. Just really really great.
@ArjanCodes
@ArjanCodes Жыл бұрын
Thanks a ton!
@LeftLib
@LeftLib 7 ай бұрын
as a newcomer I was hoping to find The Answer, what I should be using beginning from now. After watching this I am not sure, it seems odd to have a choice of 3. What would it be like 5 years from now?
@whkoh7619
@whkoh7619 Жыл бұрын
thanks for the compare! the real content is here as always ;)
@ArjanCodes
@ArjanCodes Жыл бұрын
Thanks for watching!
@SimeonNedkov
@SimeonNedkov Жыл бұрын
"Don't be an attr (etter)..." is a great way to start the week! 😁
@giovanniosorio4759
@giovanniosorio4759 Жыл бұрын
I haven’t been able to understand Enum classes and what exactly are they useful? I've been looking for videos that explain it well but there aren't any. Could you please make one?
@matercomus
@matercomus Жыл бұрын
Great video!
@ArjanCodes
@ArjanCodes Жыл бұрын
Glad you enjoyed it!
@EricChenx
@EricChenx Жыл бұрын
Thanks!
@ArjanCodes
@ArjanCodes Жыл бұрын
Thank you so much!
@Vodkarh
@Vodkarh Жыл бұрын
ok now I want to know what attrs means in dutch
@RutNij
@RutNij Жыл бұрын
The dutch word is 'etter', which is pronounced similar to attr in english. Etter translates more or less to 'jerk' in english 😂
@HanWechgelaer
@HanWechgelaer Жыл бұрын
Actually attr sounds like the Dutch word "etter" which means a brat who is acting up.🙂
@samsung40_media87
@samsung40_media87 Жыл бұрын
in afrikaans, it is pretty bad
@evandrofilipe1526
@evandrofilipe1526 Жыл бұрын
​@@samsung40_media87 what does it mean?
@JoergAsmussen
@JoergAsmussen Жыл бұрын
he is een etter -> he's a jerk (according to Google translate)
@kosmonautofficial296
@kosmonautofficial296 Жыл бұрын
When you printed that banana it totally made sense to me
@JohnFKenobi
@JohnFKenobi Жыл бұрын
great stuff
@SuperDoc3000
@SuperDoc3000 Жыл бұрын
Thanks @ArjanCodes, quality content every time!
@ArjanCodes
@ArjanCodes Жыл бұрын
Thank you!
@CrazyDalekCaan
@CrazyDalekCaan Жыл бұрын
You missed something important: pydantic has its own dataclass decorator which can be used much like a BaseModel, but is fully API compatible with a built-in dataclass
@evandrofilipe1526
@evandrofilipe1526 Жыл бұрын
I would like to know what you think about typeguard module since I never seen you use it.
@WilliS1651686868
@WilliS1651686868 Жыл бұрын
I really like the "frozen" option of dataclass that allow to have some sort of immutability. Does attrs or pedantic have something similar?
@johantoday
@johantoday 11 ай бұрын
Pydantic supports frozen fields or frozen instances
@tanjt107
@tanjt107 Жыл бұрын
I am not sure if why question is clear and related to this video. Sometimes in my classes there are a lot of lines in the init/post_init method. The reason of this is I don’t want the same thing being calculated many times and saving the result in memory can speed up the process. Maybe I should just use function instead?
@ArjanCodes
@ArjanCodes Жыл бұрын
If it's a complicated computation, I would suggest to move it out of the init method and into a separate function. You could also look into functools cached_property decorator. This computes a value once and then memoizes it.
@tanjt107
@tanjt107 Жыл бұрын
@@ArjanCodes Thanks! I never know this decorator. It helps!
@PraecorLoth970
@PraecorLoth970 Жыл бұрын
Nice video, cheers from a country where taxes aren't limited to 100% 😭
@Vijay-Yarramsetty
@Vijay-Yarramsetty Жыл бұрын
I've been having this doubt since a long time. which one among them is better?
@realplod
@realplod Жыл бұрын
Thanks
@ArjanCodes
@ArjanCodes Жыл бұрын
Thank you so much!
@OzFush
@OzFush Жыл бұрын
What brand is your hoodie? I want one but can’t find the logo in google image search 😂
@WilliS1651686868
@WilliS1651686868 Жыл бұрын
Are there any difference between those three options in terms of performance?
@jkrigelman
@jkrigelman Жыл бұрын
Now I need to learn what atter is in Dutch.
@AngDmz
@AngDmz Жыл бұрын
With pydantic you have super powerful objects factory to perform really good OOP without much effort
@ArtyomShakurov
@ArtyomShakurov Жыл бұрын
Thanks! Can you please compare pydantic and marshmallow?
@ErikS-
@ErikS- Жыл бұрын
I honestly used dataclasses for some time, but I stopped using them. I prefer having all classes written according to the standard rules for classes. So no instance variables in the part where you would standard see the class variables defined. Instead of mixing the dataclasses into regular classes, It would have been better if a new type of datastructure would be implemented in Python, and which is available by DEFAULT. Making the distinction with a decorator just doesn't do it for me... I like to say that pydantic is by the way a 'cleaner' solution imo...
@ac130kz
@ac130kz Жыл бұрын
attrs is excellent, but it's kinda not compatible with everything else, so it's an all in approach
@esmaelmohamed8209
@esmaelmohamed8209 7 ай бұрын
Arjan how I communicate you officially for your courses?
@ArjanCodes
@ArjanCodes 7 ай бұрын
To talk about my courses you can send an email to: support@arjancodes.com
@esmaelmohamed8209
@esmaelmohamed8209 7 ай бұрын
@@ArjanCodes Okay Arjan I will. ❤
@esmaelmohamed8209
@esmaelmohamed8209 7 ай бұрын
@@ArjanCodes Now I did that
@proofit404
@proofit404 Жыл бұрын
Лучший!
@Blingblingblingling
@Blingblingblingling Жыл бұрын
Curious. Why not, like price being int, make weight also int and represent the smallest unit we care about (like gram)?
@nixonkutz3018
@nixonkutz3018 Жыл бұрын
I always get tickled by the volume and popularity of code that attempts to bolt type checking onto a duck-typed language
@_Amilio_
@_Amilio_ Жыл бұрын
I forgot that "attrs" in dutch was niet zo leuk 😂
@julienyt1600
@julienyt1600 Жыл бұрын
Pydantic's Soo good to ensure that your python objects can easily become dicts, serialize in JSON, parse_raw query results and validate at the same time. I use pydantic all the time so other developpers only need to learn them and not have to deal with the differences coming in dataclasses fields
@drheaddamage
@drheaddamage Жыл бұрын
I like attrs a lot, but it is a bit funny to me that we say: "Python is great because it is dynamically typed", and then the first thing we do is strictly typing using one of these packages...
@dalaymann
@dalaymann 8 ай бұрын
한국인 중에 Pydantic 을 가르쳐 줄 수 있는 사람은 대개 미국에 살고 있으니... 감사합니다.
@HanWechgelaer
@HanWechgelaer Жыл бұрын
Nice video, now don't be a attr and like the video 😂😂😂
A Deep Dive Into Iterators and Itertools in Python
21:01
ArjanCodes
Рет қаралды 60 М.
Pydantic Tutorial • Solving Python's Biggest Problem
11:07
pixegami
Рет қаралды 260 М.
小蚂蚁被感动了!火影忍者 #佐助 #家庭
00:54
火影忍者一家
Рет қаралды 52 МЛН
Magic trick 🪄😁
00:13
Andrey Grechka
Рет қаралды 32 МЛН
EVOLUTION OF ICE CREAM 😱 #shorts
00:11
Savage Vlogs
Рет қаралды 14 МЛН
CHOCKY MILK.. 🤣 #shorts
00:20
Savage Vlogs
Рет қаралды 26 МЛН
The Ultimate Guide to Writing Classes in Python
25:39
ArjanCodes
Рет қаралды 110 М.
Python dataclasses will save you HOURS, also featuring attrs
8:50
Composition Is Better Than Inheritance in Python
23:29
ArjanCodes
Рет қаралды 257 М.
Python 3.12 Generic Types Explained
18:27
ArjanCodes
Рет қаралды 60 М.
Protocol Or ABC In Python - When to Use Which One?
23:45
ArjanCodes
Рет қаралды 200 М.
Pydantic is OP, here's why
18:10
Carberra
Рет қаралды 22 М.
Writing My Own Database From Scratch
42:00
Tony Saro
Рет қаралды 202 М.
The Ultimate Guide to Writing Functions
24:31
ArjanCodes
Рет қаралды 180 М.
小蚂蚁被感动了!火影忍者 #佐助 #家庭
00:54
火影忍者一家
Рет қаралды 52 МЛН