Python Programming from First Principles: Learn to Code, Think Computationally, and Solve Real-World Problems
PDFEPUB
BOOK 000 · CHAPTER 7 · What Is OOP?

What Is OOP?

OOP stands for Object-Oriented Programming, a programming paradigm—an approach to designing and writing programs—that organizes related data and actions into cooperating objects. Python supports OOP to help keep code structured, reusable and easier to maintain. Figure 11 shows three objects exchanging messages to handle a borrowing request in a library program.

The diagram is a conceptual sketch: each box represents a cooperating object. Follow a request from the borrowing screen to the loan service and then to the book record; the labels on the return direction show the replies. It introduces responsibilities and communication before we write the classes.

Borrowing screenLoan serviceBook recordrequest loan /return resultcheck availability /return availability

OOP offers three practical advantages:

  • Clear structure: Group related responsibilities into logical parts.
  • Reusability: Reuse shared logic instead of writing it again.
  • Easier maintenance and debugging: Keeping responsibilities separate can limit the effects of a change and help you locate problems.

A useful principle for reusable, maintainable code is DRY, short for “Don’t Repeat Yourself.” When several parts of a program need the same calculation or check, write it once and reuse it instead of copying it. If the rule changes, you only need to update it in one place [1]. For example, write one function that checks whether a book can be borrowed, then call it wherever that check is needed. If the borrowing rules change, update that function. DRY applies to functions as well as OOP.

We can now put these ideas into practice with classes and objects, the building blocks of OOP. We will start by defining a small class in Python and creating objects from it.

Reference

[1] Thomas, D., & Hunt, A. (2019). The Pragmatic Programmer: Your Journey to Mastery (20th anniversary edition). Addison-Wesley. Chapter 2, “DRY—The Evils of Duplication,” Tip 15 (p. 31).