MENU

Fun & Interesting

SAVE TIME and reduce boilerplate with dataclasses

Carberra 4,169 5 months ago
Video Not Working? Fix It Now

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 [1] https://docs.python.org/3/library/dataclasses.html [2] https://www.attrs.org/en/stable/ [3] https://docs.pydantic.dev/latest/ [4] https://youtu.be/t6Cme1WTFCI — If you want to see more of me, join my Discord server! https://discord.gg/5vVgQpK I get a lot of people asking, so here's my setup! • Visual Studio Code: https://youtu.be/PBQspYkkksU • Terminal: https://youtu.be/4RuGK3w6Mbs • Desk/recording gear: https://kit.co/Carberra • PC build: https://uk.pcpartpicker.com/list/TmzGYN Affiliations (I may earn a commission): • Codecrafters -- 40% off any plan: https://app.codecrafters.io/join?via=parafoxia • Keeper Password Manager -- 30% off your first year: https://keeper.io/r/EX4FB1C — 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

Comment