3. Object-Oriented Programming
In object-oriented programming we break a large and potentially complex program into a set of interacting elements, or objects. The idea originates from modeling (in software) real-world entities such as electrons, people, buildings, or solar systems and readily extends to modeling abstract entities such as bits, numbers, colors, images, or programs.
As discussed in Section 1.2, a data type is a set of values and a set of operations defined on those values. In Python, the values and operations for many data types such as int
and float
are predefined. In object-oriented programming, we compose code to define new data types.
This ability to define new data types and to manipulate objects holding data-type values is also known as data abstraction, and leads us to a style of modular programming that naturally extends the function abstraction style that was the basis for Chapter 2. A data type allows us to isolate data as well as functions.
- 3.1 Data Types describes how to use existing data types, for text processing image processing.
- 3.2 Creating Data Types describes how to create user-defined data types using Python's class mechanism.
- 3.3 Designing Data Types considers important techniques for designing data types, emphasizing APIs, encapsulation, immutability, and design-by-contract.
- 3.4 Case Study: N-Body Simulation presents a case study that simulates the motion of n particles, subject to Newton's laws of gravity.
Python Programs in this Chapter
Below is a list of Python programs and data files used in this chapter.