Object-oriented programming organizes data and the functions that operate on it together. Objects belong to classes, and their callable functions are methods. This replaces separate procedural lists of nouns and verbs with domain-specific types that make relationships clearer and support higher-level reasoning. section 9.0
Representing a set of scores
A procedural solution passes a list to an average function. An object-oriented solution defines ScoreList, stores the scores in an instance, and exposes average() as the operation belonging to that data. The calculation stays the same, but the caller works with a more explicit domain type. section 9.0
class ScoreList():
def __init__(self, scores):
self.scores = scores
def average(self):
return sum(self.scores) / len(self.scores)
scores = ScoreList([85, 95, 98, 87, 80, 92])
print(f'The final score is {scores.average()}.')
Shows a Python class with initialization, stored state, an instance method, and a caller using the resulting object. section 9.0
Useful object-oriented practice includes:
- Extend a Zoo class so animals can be filtered by multiple colors, transfer between two zoo instances, or queried by color and legs. section 9.40
- Use Python’s class features deliberately: __init__ initializes instances, __repr__ supplies printed representations, super() invokes parent-class methods, and @dataclass simplifies class definitions. section 9.0








