~/talk

Dataclasses: The code generator to end all code generators

Talkat PyCon 2018 · on Saturday, 2018-05-12· [python]
slides:none on file

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

  1. 0:00Start
  2. 1:34Makefile Introduction
  3. 3:22Why Do We Want a Code Generator
  4. 3:32Code Generators Reduce Wordiness
  5. 4:03What Are Data Classes for
  6. 5:22How To Use the Code Generator
  7. 10:50Type Simple Namespace
  8. 12:00Type Annotations
  9. 14:28Goals
  10. 18:51Mutable or Immutable
  11. 22:05Summary
  12. 24:08Equal Method
  13. 32:08Frozen Behavior
  14. 33:56Customize a Field Specification
  15. 34:43Add Custom Methods to the Class
  16. 36:25The Metadata Parameter
  17. 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
  18. 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
  19. 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
  20. 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
  21. 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
  22. 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
  23. 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
  24. 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
  25. 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
~/search
type to search 6,249 conferences, 138,987 speakers and 185,172 talks
~/help
cmdctrl k
search every conference, speaker and series
/
focus the filter search
h
home
c
conferences
s
speakers
e
series
t
light / dark — the status bar button does the same
?
this help
esc
close this, then clear every filter — or just the search, while you are typing in it

Ignored while you are typing. Pages without a filter rail have no search to focus.