Chapter 8
Small Objects, Records, and Type Hints
Objects, attributes, small classes, methods, dataclasses, function type hints, and configuration records.
What this chapter does
Modern ML code often passes named bundles of settings and results. This chapter teaches just enough objects and type hints to read and write those records without turning object-oriented programming into a separate subject.
Lessons
Read these in order.
The chapter opening gives the main idea. Move through these lessons next; each page reuses ideas from the pages before it.
- 01Objects and Attributes
Dot notation, attributes, methods on built-in values, and AttributeError.
- 02Small Classes
Classes as object shapes, initializers, self, and when a class is useful.
- 03Methods
Functions attached to objects and when behavior belongs near the data.
- 04Dataclasses
Compact record definitions, field hints, generated initializers, and readable printed objects.
- 05Type Hints for Functions
Parameter hints, return hints, simple container hints, and what hints do not enforce.
- 06Configuration Records
Dataclasses for named experiment settings passed into small computations.
- •Conclusion
How small records prepare readers for array objects and experiment settings.
- •Review
A compact review of attributes, small classes, methods, dataclasses, type hints, and configuration records.
- •Exercises
Chapter-level practice for attributes, dataclasses, methods, type hints, and configuration records.
You are ready when
- Read object attributes with dot notation.
- Define a small class with an initializer.
- Add a method when behavior belongs with the data.
- Use dataclasses for simple records.
- Read and write simple function type hints.
- Pass a configuration record into a computation.
Where this leads
- NumPy Arrays
- Array Computation
- Small Numerical Experiments