O'Reilly logo
live online training icon Live Online training

Working with Dataclasses in Python 3.7

Scott Irwin

Classes have been available in Python since the beginning but writing a fully featured data-centered class required developers to write and test multiple special methods (a.k.a. dunder methods). In addition to having to be written initially, these methods needed to be maintained as the class gained or lost data fields.

In Python 3.7 the dataclasses module was introduced to the standard library (also available as a pip installable module for Python 3.6). This module makes it easier for developers to create user-defined classes, particularly custom data containers, by reducing the amount of code that they need to write, test, and maintain.

This Live Training will help you explore and understand the features of the dataclasses module so that you can effectively use it to write better, more maintainable Python classes.

What you'll learn-and how you can apply it

You will understand:

  • How dataclasses reduces the effort needed to create fully featured user-defined classes, particularly custom data containers
  • How dataclasses compare to existing Python data structures

You will be able to:

  • Use the basic dataclasses to reduce the amount of effort needed to write Python classes
  • Use the additional features of dataclasses to fine-tune and further customize your classes

This training course is for you because...

You are a developer with programming experience in Python who wants to write fully featured classes using less code.

Prerequisites

Familiarity with Python class and class decorator syntax

Familiarity with the topics described in the following lessons and chapters: - Python Programming Language LiveLessons (video): Lesson 7, 9, 12 - Learn Python 3 the Hard Way (book): Exercises 40-43 - Head First Python, 2nd Edition (book): Chapter 8

Course Set-up

  • A machine with Python 3.7 (or above) installed and the ability to run a code editor (VS Code, vim, emacs) or a Python IDE (PyCharm, IDLE)
  • Clone GitHub repository with starter code

About your instructor

  • Scott is a Senior Software Engineer at Bloomberg LP where he both develops business critical applications using Python and trains other developers in the Python language.

Schedule

The timeframes are only estimates and may vary according to how the class is progressing

Segment 1 Introduction to Dataclasses Length: 10 min

  • Instructors will… give some background and an overview of dataclasses
  • Participants will… ask questions

Segment 2 Basic Features Length: 30 min

  • Instructors will… discuss and explore the features of a simple dataclass
  • Participants will… create and explore a simple dataclass

Break (length: 10 min)

Segment 3 @dataclass decorator Length: 25 min

  • Instructors will… discuss and explore the features of the @dataclass decorator
  • Participants will… create and explore dataclasses defined using the @dataclass decorator options

Segment 4 field() function Length: 25 min

  • Instructors will… discuss and explore the features of the field() function
  • Participants will… create and explore dataclasses defined using the field() function

Break (length: 10 min)

Segment 5 Additional features Length: 35 min - Instructors will… discuss and explore the some of the additional features of the dataclasses module (e.g., post-init processing) - Participants will… create and explore dataclasses using the additional features

Segment 6 Comparison to existing data structures Length: 15 min - Instructors will… give an overview how dataclasses to compare to existing data containers - Participants will… ask questions

Segment 6 Wrap up and Q&A Length: 10 min - Instructors will… summarize the use case for dataclasses - Participants will… ask questions