SAVE TIME and reduce boilerplate with dataclasses

preview_player
Показать описание
If you're looking for ways to get rid of boilerplate in Python to save your precious time, dataclasses are for you! You just need to provide the attributes you need, and the dataclasses will create the constructor, figure out the string representation, and more. Perfect for speeding up your workflow!

I cover quite a lot in this video, so I've made the chapters below very granular so you can find exactly what you're looking for.

00:00 - Intro
00:53 - The basics of dataclasses [1]
03:19 - Defining default arguments
04:48 - Using our dataclass
05:39 - The init and repr options
05:54 - Customising equality and ordering checks
07:02 - Unsafe hashing and freezing dataclasses
08:58 - Changing constructor argument behaviour
09:32 - Autogenerating slots
09:41 - Excluding fields from the constructor
09:58 - Removing fields from the string representation
10:14 - Customising hash behaviour at field level
10:48 - Field-level equality and ordering checks
11:00 - Field metadata
11:13 - Setting fields as keyword-only arguments
13:05 - Overview of potential altenatives [2-4]
13:39 - Post-initialisation and InitVar
18:14 - Outro



If you want to see more of me, join my Discord server!

I get a lot of people asking, so here's my setup!

Affiliations (I may earn a commission):



If you have any questions, don't hesitate to ask in the comments! I'll try and answer as soon as I can, providing someone else hasn't already done so.

#python #coding #howto
Рекомендации по теме
Комментарии
Автор

Hm, at 01:32 I'm confused a little - at least ... Wouldn't be the 3rd line something like "DIVERSE" the preferred one followed by "USER_SPECIFIED" ?

dragonfly-
Автор

Haven't verified it, but having different hashes in the same object per execution should be necessary for security reasons, wouldn't it?

saisuapalli
Автор

Are you programming straight on windows? I only program on WSL. Programming on Windows is silly because you are gonna deploy to linux. You're creating extra heacaches when you need to port that from windows to linux. The vscode remote extension let's you code in WSL, on remote servers, and straight into Docker containers. I got my team off coding straight on windows and into WSL back in 2019.

cbernier
Автор

dataclasses aren't new though. They came out in 2018.

cbernier
welcome to shbcf.ru