Dataclasses: The code generator to end all code generators
summary
PEP 557 dataclasses (in Python 3.7) generates __init__, __repr__, __eq__ and other methods so less boilerplate and higher‑quality defaults; it uses annotations, supports defaults and default_factory, provides replace(), asdict(), and fields() metadata, and can be mutable by default or turned frozen and orderable. It trades size for speed (example given: 168 bytes vs 72 bytes and field access ~33ns vs ~61ns), hides generated code (uses exec), has slots and inheritance edge cases, and is inspired by attrs and typing.
AI-written summary · about
chapters (25)
- 0:00Start
- 1:34Makefile Introduction
- 3:22Why Do We Want a Code Generator
- 3:32Code Generators Reduce Wordiness
- 4:03What Are Data Classes for
- 5:22How To Use the Code Generator
- 10:50Type Simple Namespace
- 12:00Type Annotations
- 14:28Goals
- 18:51Mutable or Immutable
- 22:05Summary
- 24:08Equal Method
- 32:08Frozen Behavior
- 33:56Customize a Field Specification
- 34:43Add Custom Methods to the Class
- 36:25The Metadata Parameter
- 40:05So We Needed All this Power in Addition the Fields Functions Lets Us Introspect Are the Field Specifications and We Can See that the Salary Field Are the Payment Is in a Bitcoin the Code That's Generated Here Is this Is Very Close to What You Would Write by Hand for the Anit To Create a New List the Repper Is What You Would Write by Hand the Equality's We Discussed before Is Type Specific as Are the Other Comparison Functions What's Interesting about the Comparison Functions Is Not How They'Re Written Is What They Exclude
- 40:16The Code That's Generated Here Is this Is Very Close to What You Would Write by Hand for the Anit To Create a New List the Repper Is What You Would Write by Hand the Equality's We Discussed before Is Type Specific as Are the Other Comparison Functions What's Interesting about the Comparison Functions Is Not How They'Re Written Is What They Exclude They Don't Include all of the Fields and Likewise the Hash Doesn't Include all of the So some Quick Closing Thoughts and Then To Defend Myself during the Question and Answer Period I'Ve Called In for Backup
- 40:43So some Quick Closing Thoughts and Then To Defend Myself during the Question and Answer Period I'Ve Called In for Backup and Reinforcements so if I either Tell You any Lies Are Done to the Answer the Person Who Knows the Truth and May Be Willing To Sell It Share It with You Is Here So I'Ll Invite to Eric To Come On Up and We'Ll Give Him a Round of Applause When He Gets Here So Here Are the Challenges with Using It One Is It's Currently Tricky To Add Slots to a Data Class and Slots Is a Pretty Darn Important Optimization It'Ll Save You a Lot of Space and Speed and Prove the Speed of Access of the Fields
- 40:55The Person Who Knows the Truth and May Be Willing To Sell It Share It with You Is Here So I'Ll Invite to Eric To Come On Up and We'Ll Give Him a Round of Applause When He Gets Here So Here Are the Challenges with Using It One Is It's Currently Tricky To Add Slots to a Data Class and Slots Is a Pretty Darn Important Optimization It'Ll Save You a Lot of Space and Speed and Prove the Speed of Access of the Fields so Slots Works Just Fine until You Have Default Values and for Deep Reasons They Fight each Other this Is Hard To Fix
- 41:09With Using It One Is It's Currently Tricky To Add Slots to a Data Class and Slots Is a Pretty Darn Important Optimization It'Ll Save You a Lot of Space and Speed and Prove the Speed of Access of the Fields so Slots Works Just Fine until You Have Default Values and for Deep Reasons They Fight each Other this Is Hard To Fix but Probably Will Be Fixed if We Fix It though Almost Certainly We'Re Going To Have To Have the Classic Decorator Not Modify the Class in Place but Make a Brand New Class this Will Be Irritating at So Many Levels the Dunder and Nit That's Generated Doesn't Call Super because Most of the Time That's Not What We Want but if You Incur It from a Classes and It Does To Be To Call There's a Interesting Gundermutt They Call Post in It That Calls What You Need There Was some Debate about whether To Include a Pre in It
- 41:34Not Modify the Class in Place but Make a Brand New Class this Will Be Irritating at So Many Levels the Dunder and Nit That's Generated Doesn't Call Super because Most of the Time That's Not What We Want but if You Incur It from a Classes and It Does To Be To Call There's a Interesting Gundermutt They Call Post in It That Calls What You Need There Was some Debate about whether To Include a Pre in It but that Probably Won't Happen unless
- 42:10We Don't Have an Easy Way To Overwrite or Extend that under New Method It Is Really All about Things That We'Re Putting the Data in So I Tried To Reinvent Name Tupple Using Data Classes and It Essentially Did Nothing for Me Other than Create the Repper and Not in the Way It's Clean Away as the Name Couple Did So There's some Things That It's Not Good at Mostly It's Good for a Lot of Things Future Directions I Expected over Time People Will Request Things That Are in the Adders Package Will Come To Be Present in Data Classes
- 43:19The Challenge for You Now Is that Was a Nice Round of Applause I Challenge You To Do Better I'D Like You To Double that for the Person Who Actually Did All the Work Eric Do You Want To Come Up with Me You'Ll Need a Microphone So Right Derek Smeath We Have Time for About One Question So Make It a Good One Hi Raymond Thanks for the Talk There's One Thing I Can Do if I Hand Write that Under in It I Can Make Arguments Key Word Only by Putting in an Asterix in the Function Signature Is There any Support for that in Declarative Data Clause I'Ll Say No and Then I Will Get the Right Answer There's Not
- 44:33If You Inherit from Something That Had Only Keyword Arguments and Everything You Had Would Suddenly Have To Become Keyword Arguments if You Have a Data Class Inherits from another Data Class It Adds that under an It to the Child Class It Just Depends all of the Fields Together on the Inherited Classes Dunder Init so It Has To Have Everything in It because It Doesn't Call the Base Class Executive Summary It's Harder than It Looks All Right Let's Give Them One Last Round of Applause
related talks
26mEverything you need to know but were afraid to ask about Data Classes
32mEasier Classes: Python Classes Without All the Cruft
22mEasier Classes: Python Classes Without All The Cruft
33mData Class Serialization The Right Way
30mThe Wizardry of Metaprogramming
29mMaking Data Classes Work for You
01h 10mIt's Pythons All The Way Down: Python Types & Metaclasses Made Simple
47mA Deep-Dive Into Python Classes
