Concepts / Object-Oriented Programming Principles

Object-Oriented Programming Principles

Procedure-oriented programming organizes code around reusable functions; object-oriented programming organizes code around objects that combine data and functionality.

  • Programming

Two Ways to Organize a Program

Imagine a program that works with a student's name, ID, and grades. One design starts with the actions the program must perform: calculate an average, print information, and update a grade. Another design starts with the thing those actions belong to: a student. The first design is procedure-oriented; the second is object-oriented. The important difference is not that one design has functions and the other does not. Both can use functions. The difference is what the program treats as its main organizing unit.

Procedure-oriented programming is organized around reusable functions. Object-oriented programming is organized around objects that combine data and functionality.

Procedure-Oriented Organization

In procedure-oriented programming, you think of the program as a collection of reusable procedures or functions. A function takes data as input, processes or transforms it, and returns a result. The data is separate from the functions, so data flows between function calls. The focus is on what actions the program performs.

data inputdata inputdata inputcontainscontainsoperates onStudent dataname, ID, gradescalculate_averagefunctionStudent objectdata and methodsStudent dataname, ID, gradesprint_student_infofunctionStudent methodsfunctions for the dataupdate_gradefunction
How does the same student-related program differ when organized around separate functions or around an object that keeps related data and functionality together?

Using the source scenario, a procedure-oriented design might keep the student's information separate while calling calculate_average(grades), print_student_info(name, id, grades), and update_grade(grades, new_grade). Each function is independent and reusable, but information about the student is spread across the different function calls.

Objects as Cohesive Units

In object-oriented programming, you think of the program as a collection of objects. An object bundles the data it cares about with the functions that operate on that data. The data is called attributes, and the functions inside the object are called methods. This creates a cohesive unit: the object's information and the operations closely related to that information are considered together.

containscontainsincludesincludesoperates onoperates onStudent objectone cohesive unitAttributesname, ID, gradesAverage operationuses gradesMethodsfunctions for student dataGrade updatechanges grades
What does an object contain, and how are its data and functions connected?

Reorganizing Student Information

Compare two ways to represent a program that stores a student's name, ID, and grades and performs operations on that information.

Start with the procedure-oriented view: Treat the program as reusable functions. The functions receive the student's data, process it, and return or display results. The data remains separate from those functions.

Change the organizing unit: Treat the student as an object. Place the student's data inside the object and associate methods with the operations performed on that data.

Check the relationship: The object now represents a student-related unit, while its methods represent functionality that operates on that object's attributes.

The procedure-oriented design emphasizes actions and data passed between functions. The object-oriented design emphasizes the student as a thing in the program and keeps its related data and functionality together.

Objects Working Together

Object-oriented design is not only about placing data and methods inside one object. Objects can also represent multiple similar things and interact with each other. When one object uses another object's functionality, the interaction is organized around the objects and the roles they play. This differs from a procedure-oriented flow in which data is passed from one function to another for processing.

requests functionalityprovides functionalityproducesStudent objectstudent dataMethod useone object usesfunctionalityProcessed resultreturned or passed onwardRecord objectrelated data
How does data move between objects when one object uses another object's functionality?

The diagram shows the general idea rather than a required Python implementation. The key point is that object-oriented programs can model several related entities, give each entity its own data and functionality, and organize interactions around those entities.

Choosing an Organization

Object-oriented design is especially useful when a program needs to model real-world entities, create multiple similar things, or let related data and behavior stay together. These qualities can make an object-oriented organization a good fit for systems where many entities interact. Procedure-oriented design remains useful when the problem is naturally expressed as a collection of reusable functions that receive, transform, and pass along data.

QuestionProcedure-oriented emphasisObject-oriented emphasis
What is the main organizing unit?Reusable procedures or functionsObjects
Where is the data?Separate from the functionsBundled with the object's functionality
What is the program's focus?What actions the program performsWhat things exist and how they interact
How does processing occur?Functions receive, transform, and pass along dataMethods operate on the data associated with objects

A comparison of the organizing ideas described in the source material.

Python's Flexible Approach

Python fully supports procedure-oriented programming: you can write pure functions and organize an entire program around them. Python also supports object-oriented programming, in which objects bundle attributes and methods. Because Python supports both paradigms, a Python program does not have to follow only one style. You can choose or mix approaches based on the problem's needs.

What do you think happens?

A Python program needs reusable functions, but some data and the functionality that operates on it also form a natural unit. Must the entire program use only one paradigm?

  • Yes, Python requires one paradigm for the whole program.
  • No, Python supports both paradigms and allows them to be chosen or mixed.
  • Yes, Python only supports object-oriented programming.
Reveal answer

Answer: No, Python supports both paradigms and allows them to be chosen or mixed.

Python supports pure functions and procedure-oriented organization, as well as objects that combine data and methods.

Mistakes About the Two Paradigms

  • Thinking procedure-oriented programming cannot use reusable functions.

    Procedure-oriented programming is explicitly organized around reusable procedures or functions.

    Fix: Remember that both approaches can use functions; they differ in what organizes the program.

  • Assuming object-oriented programming means storing data without functionality.

    In OOP, the object's data and the functions that operate on it are bundled together.

    Fix: Look for both attributes and methods when identifying an object-oriented unit.

  • Assuming Python forces every program to be object-oriented.

    Python fully supports procedure-oriented programming and allows programs to be organized around pure functions.

    Fix: Choose or mix paradigms according to the problem's needs.

  • Comparing the paradigms only by the presence of functions.

    The more important distinction is whether the program is organized around actions and data flow or around objects and their interactions.

    Fix: Ask what the program treats as its main organizing unit.

Apply the Distinction

MEDIUM

A program manages several similar entities. Each entity has data of its own, and several operations repeatedly work with that related data. Explain which paradigm may provide a natural organization and why. Then describe a situation in which organizing the work as reusable functions that receive and transform data could be appropriate.

Hints
  • Identify whether the program is primarily organized around actions or around things that exist.
  • Look for data and functionality that belong together.
  • Remember that Python allows either approach or a mixture.

A strong explanation should mention the organizing unit, the relationship between data and functionality, and the way the program's parts interact.

Key Takeaways

  • Procedure-oriented programming organizes code around reusable functions, with data passed between those functions.
  • Object-oriented programming organizes code around objects that combine attributes with methods.
  • Objects can model real-world entities, represent multiple similar things, and interact with one another.
  • Python supports both procedure-oriented and object-oriented programming and allows the approaches to be mixed.
  • Choose an organization according to whether the problem is better expressed through actions and data flow, objects and interactions, or a combination.