Which Python @dataclass is best? Feat. Pydantic, NamedTuple, attrs...

preview_player
Показать описание
Get rid of boilerplate in writing classes.

Which dataclass alternative should you use though? In this video we test dataclasses, attrs, tuple, namedtuple, NamedTuple, dict, SimpleNamespace, and Pydantic BaseModel for speed, memory efficiency, and features.

SUPPORT ME ⭐
---------------------------------------------------

Top patrons and donors: Jameson, Laura M, Dragos C, Vahnekie, John Martin, Casey G, Pieter G, Krisztian M, Sigmanificient

BE ACTIVE IN MY COMMUNITY 😄
---------------------------------------------------

CHAPTERS
---------------------------------------------------
0:00 Intro
1:04 dataclass
1:24 attrs
2:13 tuple, namedtuple, NamedTuple
4:05 dict
4:39 SimpleNamespace
4:58 Pydantic
6:39 Speed comparison
8:41 Memory comparison
9:15 Feature matrix and winners
Рекомендации по теме
Комментарии
Автор

I'm a simple engineering student and a modest python user but those dynamic histograms sent chills down my spine

LiamInviteMelonTeee
Автор

The buildin dataclass also has default_factory for defining default mutable values .

aarondewindt
Автор

This is a great breakdown. I’ve had to explain this so many times to team members, now I’ll refer people to this video!

mpilosov
Автор

Hey James, I just wanted to sincerely congratulate you for both the quality content and humor in your videos, amazing work!!

franchiniitalo
Автор

I actually have a large ongoing project where I used namedtuples early on, with typing stored in a second tuple, then refactored to NamedTuple using the built in typing (which simplified storing the typing separately), and finally to dataclasses after seeing your video on that. It fit my application perfectly as I needed the flexibility of being able to modify the dataclass. If only I had known about dataclasses to start with. :) attr class also sounds interesting for my needs, I need to check that out.

markasiala
Автор

I really enjoyed the text comment/annotation overlays in this video. they both add useful background info and give the video a more relaxed vibe without distracting from the main points! :D

jemand
Автор

So when _are_ you going to explain slots? I have no idea what those are

laurinneff
Автор

2:06 got me .. "real life". Those air quotes are heavy.

JakubYTb
Автор

Please keep the onscreen comments coming! Adds the perfect amount of fun to an informative topic "cries in mypy" 😁
Great video, thanks 😊

jochengietzen
Автор

Great thanks so much for this video! I am starting to study pydantic and I haven't been made aware of these differences. This is a huge help and I wish more people would explain these important differences when telling others they should use this or that.

kosmonautofficial
Автор

I've used most of these, it turns out.
Started with the class that repeats everything. I then used the dict to try and make things slightly more convenient, but that was only feasible in very limited circumstances.
Then I discovered named tuples, but... they are tuples. Wasn't a huge fan.
Then, finally, I came across attr. That was a huge revelation and I absolutely loved it. Finally something decent.
And then dataclasses were introduced to the standard library and I basically switched to using those. attr can do more, sure - but the dataclasses are easier to use and don't need the dependency. Unless I actually need the power of attr, I'll just use these.

Yotanido
Автор

Also would have loved thoughts on TypedDict which mirrors NamedTuple for dictionaries, giving type hinting and string key checking

Aang
Автор

At first you got my interest, after the "Presenting with meaningless example" you got my attention. Awesome video once again!

hackergr
Автор

I've often struggled with ways people define hyperparameters and inputs to neural networks in open source code. This video definitely helped me in my choice going forward.

sphereron
Автор

this is really the best Python channel on YouTube. I've learned more on this channel than all others combined

r_rho
Автор

Yeah pyndatic is so great for parsing/Serializing Json data.
I've been using it. But for simple data, I use built in dataclass

zacky
Автор

Iʼm starting a new Python project and Iʼm using `attrs` because of this video. Otherwise I would have used `namedtuple`, because I think without your videos I somehow would have missed even `dataclas`.

danielrhouck
Автор

This is serious pro stuff. I started using dataclass thanks to your vid, then YT pushed another vid for pydantic and I was like bleh. Luckily this vid set me striaght. Now I understand each's use case. THANKS

doccore
Автор

4:36 There's TypedDict to consider too tho. (As in the type safety thing. You could type both dict and tuple and use a static type checker. If you use PyCharm and single quotes, accessing data by key is also not typo prone.)

maimee
Автор

Type hint gang. I came from a C background, so ducktyping felt like a Godsend. Then I got into rust and realized how much time I was spending debugging code because it was ducktypable. (lets not forget rust's awesome toolchain compared to python's...well yeah.) Hell, I was using i_var, etc just because it made it easier to reason about and not have to backtrack, which is when I first started wondering about it...Didn't fully click until I made the switch though.

GRAYgauss