10 Objects
This chapter introduces Python’s object model and shows why bundling data (nouns) with behavior (verbs) into objects clarifies program structure and intent. It explains how defining domain-specific types raises the level of abstraction, shortens communication, and separates interface from implementation, while also cautioning that object orientation is not a cure‑all and can be misused (e.g., giant classes or tangled hierarchies). Although Python mixes paradigms freely, “everything is an object,” and the language’s consistency lets once-learned rules apply broadly.
You learn the core mechanics of classes, instances, attributes, and methods; how __init__ sets state after construction; and why self is an explicit first parameter. The text contrasts procedural and object-oriented styles, covers representation methods, and distinguishes instance versus class attributes, including using class attributes as configuration-like “constants.” It emphasizes composition (“has‑a”) over inheritance (“is‑a”), then shows judicious inheritance with super and method overriding. Attribute lookup follows a predictable search path (instance → class → parents → object), and dataclasses are presented as a way to reduce boilerplate while keeping Python’s dynamic feel.
Hands-on exercises progress from simple to rich models: a Scoop and Bowl to practice attributes, composition, and class-level limits; a BigBowl subclass that changes behavior by overriding only configuration; a FlexibleDict that subclasses dict and overrides __getitem__ to demonstrate customizing built-ins; and a small zoo domain (Animals, Cages, Zoo) that combines inheritance, composition, and Pythonic iteration (comprehensions, generator expressions) to query and aggregate data. By the end, you can design small, cohesive types, reuse behavior through inheritance where it fits, prefer composition for structure, and comfortably mix object-oriented, procedural, and functional techniques in readable Python code.
Three instances of Scoop, each referring to its class
Our three instances of Scoop in a list
A new instance of Bowl, with an empty list of scoops
Three Scoop objects in our bowl
max_scoops sits on the class, so even an empty instance has access to it.
A Bowl instance containing scoops, with max_scoops defined on the class
Wolf inherits from Animal. Notice which methods are defined where.
A Cage instance containing one wolf and one sheep
Summary
Object-oriented programming is a set of techniques, but it’s also a mindset. In many languages, object-oriented programming is forced on you, such that you’re constantly trying to fit your programming into its syntax and structure. Python tries to strike a balance, offering all of the object-oriented features we’re likely to want or use, but in a simple, nonconfrontational way. In this way, Python’s objects provide us with structure and organization that can make our code easier to write, read, and (most importantly) maintain.
Python Workout, Second Edition ebook for free