Introduction to Object-Oriented Programming
Procedure-oriented programming organizes code around reusable functions; object-oriented programming organizes code around objects that combine data and functionality.
Two Ways to Organize a Program
Imagine a program that manages student information. It must store a student's name, ID, and grades, calculate an average, display the student's information, and update a grade. There are two broad ways to organize this work. A procedure-oriented design starts with the actions the program performs. An object-oriented design starts with the things that exist in the program and the behavior connected to those things.
What do you think happens?
A design stores a student's name, ID, and grades separately, while several reusable functions receive that information as input. Which programming approach does this describe?
Reveal answer
Procedure-oriented programming
Procedure-oriented programming organizes code around reusable procedures or functions. The data is separate from the functions and flows between them.
Following Data Through Procedures
In procedure-oriented programming, you think of the program as a collection of reusable procedures or functions. A function takes data as input, transforms it, and returns a result. The data itself remains separate from the functions. The focus is on what actions the program performs and how data moves from one function to another.
Student information as procedures
Organize operations for a student's name, ID, and grades using a procedure-oriented design.
- 1Store the data
Keep the student's name, ID, and grades as data separate from the functions.
- 2Calculate
A function such as
calculate_averagereceives the grades and produces an average. - 3Display
A function such as
print_student_inforeceives the student's name, ID, and grades and displays the information. - 4Update
A function such as
update_gradereceives the grades and a new grade, then processes the grade data.
The program is organized around reusable actions. The student's data flows between separate functions.
Bundling Data with Behavior
In object-oriented programming, you think of the program as a collection of objects. Each object bundles together the data it cares about and the functions that operate on that data. The data is called attributes, and the functions inside the object are called methods. This makes the object a cohesive unit: the student's information and the operations that use that information belong together.
Student information as an object
Organize the same student's information using an object-oriented design.
- 1Create the object
Represent the student as an object that contains the student's name, ID, and grades as attributes.
- 2Attach behavior
Place operations such as calculating an average, displaying information, and updating a grade with the student object's data as methods.
- 3Use the cohesive unit
Work with the student object and its methods instead of passing the student's separate pieces of data through every operation.
- 4Represent similar things
The same object-based idea can represent multiple similar students, with each object containing its own student data and related methods.
The program is organized around student objects that combine the data they care about with the functions that operate on that data.
Choosing the Program's Center
The two approaches emphasize different questions. Procedure-oriented programming asks what actions the program needs to perform and organizes reusable functions around those actions. Object-oriented programming asks what things exist in the program, what data each thing cares about, and how those things interact.
| Procedure-oriented programming | Object-oriented programming |
|---|---|
| Organizes code around reusable procedures or functions. | Organizes code around objects. |
| Focuses on what actions the program performs. | Focuses on what things exist and how they interact. |
| Data is separate from functions and flows between them. | Data and the functions that operate on it are bundled in an object. |
| A function receives, transforms, and returns data. | An object's methods operate on the data held by that object. |
The central organizational difference between the two paradigms.
Python's Flexible Approach
Python supports both procedure-oriented and object-oriented programming. You can write pure functions and organize an entire program around them. You can also organize a program around objects whose attributes and methods stay together. Python therefore lets you choose an approach or mix both approaches according to the needs of the problem.
For example, a Python program could use separate reusable functions for a simple sequence of data transformations, while using objects to represent multiple entities that have their own data and related behavior. The key idea is not that one paradigm replaces the other; Python allows both styles.
Common Design Mistakes
Common mistake Treating object-oriented programming as merely putting every function inside an object.
What it looks likeA design places unrelated functions together without connecting them to the object's data.
Why it failsThe defining idea is that an object bundles the data it cares about with the functions that operate on that data.
Do this insteadAsk which data belongs to the object and which methods meaningfully operate on that data.
Common mistake Assuming procedure-oriented programming cannot use reusable functions.
What it looks likeA learner describes reusable functions as an object-oriented feature.
Why it failsProcedure-oriented programming is organized around reusable procedures or functions.
Do this insteadRemember that both approaches can use reusable functionality; they organize the relationship between data and functionality differently.
Common mistake Assuming Python requires object-oriented programming for every problem.
What it looks likeA learner tries to represent a simple sequence of transformations as interacting objects without first considering the problem's needs.
Why it failsPython fully supports procedure-oriented programming and allows programs to be organized around pure functions.
Do this insteadChoose or mix paradigms based on the problem.
Common mistake Confusing an object with only its data.
What it looks likeA student object is described as only a name, ID, and grades.
Why it failsIn OOP, the object also contains the methods that operate on its data.
Do this insteadDescribe both the object's attributes and its related methods.
Practice the Distinction
A library program represents many books. Each book has information about itself, and operations are needed that work with that book's information. Explain how you would describe this design in procedure-oriented terms and in object-oriented terms. Then state which approach more directly models the books as entities and why.
Hints
- For the procedure-oriented description, focus on reusable functions and data passed between them.
- For the object-oriented description, identify the data belonging to a book and the methods that operate on it.
- Use the idea of multiple similar things interacting when explaining your choice.
- 1Procedure-oriented programming organizes a program around reusable functions, with data flowing between those functions. Object-oriented programming organizes a program around objects that combine attributes and methods. Objects are useful for modeling real-world entities and multiple similar things that interact. Python supports both approaches, so you can choose one or mix them according to the problem.
Key Takeaways
- Procedure-oriented programming centers on reusable procedures or functions and the actions they perform.
- Object-oriented programming centers on objects that bundle attributes with methods.
- The main difference is how the program organizes the relationship between data and functionality.
- Objects are useful for modeling entities and multiple similar things that interact.
- Python supports procedure-oriented programming, object-oriented programming, and combinations of both.