liveBook · Manning

Object-oriented programming

As explained in Python Workout, Second Edition

Object-oriented programming is a programming paradigm that organizes software around objects that combine state and behavior.

Object-oriented programming organizes data and the functions that operate on it into objects. A class defines a type, an instance represents a particular object, and methods provide the operations associated with its data. section 10.0 section 10.1

Start with a concrete design question: should a program pass a generic list to a separate function, or create a domain-specific type that owns the relevant operation? A ScoreList keeps its scores and exposes average() through a focused interface. The calculation can change later without changing callers. section 10.1 section 10.2

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()}.')

Use the ScoreList implementation to trace initialization, instance attributes, method calls, and the shift from a generic list to a domain-specific object. section 10.1

When designing or reviewing an object-oriented solution:

  • Give each object a distinct responsibility so planning, maintenance, and collaboration become easier. section 10.2
  • Treat the public methods as the interface, while allowing the underlying implementation to evolve. section 10.2
  • Use inheritance for related types when shared parent functionality genuinely reinforces their relationship. section 10.2
  • Combine object-oriented and functional techniques when methods become shorter and clearer, rather than treating either style as universally superior. section 10.34
  • Remember that Python data is public; document the API and account for attributes that may change or be deprecated. section 10.34

A domain-specific score object

A procedural solution stores scores in a list and passes that list to average(). An object-oriented solution constructs ScoreList with the scores and calls scores.average(), preserving the same calculation while making the domain relationship explicit. section 10.1

Practice by creating classes with methods and attributes at both object and class levels, then extend them through composition and inheritance. These exercises build the ability to write and maintain Python code and clarify how built-in types are structured. section 10.3

How Python Workout, Second Edition covers it

How deep it goes
Defines classes, instances, methods, attributes, interfaces, inheritance, composition, and public-data tradeoffs through implementation exercises.
Examples
Uses student grade averages, ScoreList, and a Zoo class combining object-oriented methods with list comprehensions and generator expressions.
What it assumes
Assumes familiarity with Python’s core data structures while teaching object-oriented organization through progressive exercises.
What you can do after
Build and maintain Python classes using methods, attributes, composition, inheritance, and documented interfaces.
How it connects
Connects classes with methods, attributes, abstraction, encapsulation, inheritance, composition, and functional programming.

Also explained in

Compare how 9 books explain object-oriented programming →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Python Workout, Second Edition. The section links point to the parts of the book it draws on.