Classes and Dataclasses
Describe your own kinds of things. You'll write classes with attributes and methods, let dataclasses write the routine parts for you, and build one class on another with inheritance. Then you'll write a houseplant care app that knows which plants need water today, and finish Module 3 ready for its contact manager project.
Updated
Classes in Python
- Classes: Blueprints for Your Own Things (Apprentice): A class describes one kind of thing your program keeps track of, what it holds and what it can do, so every object made from it gets the same attributes, the same methods and a friendly printout.
- Dataclasses: Classes That Hold Data (Journeyman): The @dataclass decorator writes a data-holding class's __init__, __repr__ and == for you from a list of fields with types, with defaults for the fields a call can leave out and default_factory for a fresh list in every object.
Inheritance in Python
- Inheritance: One Class Built on Another (Journeyman): A subclass such as class Cactus(Plant) gets every attribute and method of the class it's built on, sets itself up with super().__init__(), replaces only the methods that work differently, and isinstance() tells which kind an object is.
Exercise: Houseplant Care
- Exercise: Houseplant Care (Journeyman): Finish a houseplant app's Plant class, a Cactus subclass that rests in winter, a CareLog dataclass and a function that lists today's plants to water, and let pytest's tests check each one.
Up Next: Testing and Quality
- Up Next: Testing and Quality (Apprentice): Module 3 gave you the tools of real programs (modules and packages, virtual environments, exceptions and the debugger, text, CSV and JSON files, and classes); this lesson shows which tool fits which job, a contact manager you can build with all of them, and what Module 4 adds.