Concepts / Object-Oriented Programming Fundamentals

Object-Oriented Programming Fundamentals

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 manages student information. One design begins with the actions the program must perform: calculate an average, print student information, and update a grade. Another design begins with the thing being represented: a student that contains identifying information, grades, and functionality for working with that information. These are two different ways to organize the same kind of program.

Procedure-oriented programming organizes code around reusable procedures or functions. Data flows between those functions as each function processes it and passes results onward. Object-oriented programming organizes code around objects. Each object combines the data it cares about with the functions that operate on that data. The procedure-oriented focus is what actions the program performs; the object-oriented focus is what things exist and how they interact.

FunctionsReusable actionsStudent dataPassed between functionsObjectsData plus methodsStudentAttributes and methodsprocessescontains
How does the organization of code differ when the same program is structured around reusable functions versus objects?

Following the Data

Worked example

One Student in Two Designs

Organize operations for a student's name, ID, and grades using a procedure-oriented design and an object-oriented design.

  1. 1
    Procedure-oriented organization

    The student's name, ID, and grades remain separate from the functions. A calculation function receives grades, an information-printing function receives the name, ID, and grades, and an update function receives grades and a new grade.

  2. 2
    Data movement

    The relevant student data is supplied to each function that needs it. The functions transform or use the data and return results or updated information.

  3. 3
    Object-oriented organization

    A student object groups the student's data with methods that operate on it. The object becomes the unit representing that student.

  4. 4
    Multiple students

    The object-oriented organization can represent multiple similar things as separate student objects that can interact with the rest of the program.

Result

The procedure-oriented design follows actions and passes data between functions. The object-oriented design follows student entities and keeps each entity's data and related functionality together.

Student dataName, ID, gradesAverage functionProcesses gradesPrint functionUses student informationUpdate functionProcesses a new gradegradesresultdata
How does student information move through a sequence of reusable functions?

Inside an Object

Definition

In object-oriented programming, data stored by an object is called attributes. Functions that operate on the object's data are called methods. The object bundles its attributes and methods into one unit.

The important change is not merely giving data a new label. The program's main unit of organization changes. Instead of asking which function should receive a separate collection of data, you can think about which object owns or represents that data and which methods belong with it. For a student object, the student's name, ID, and grades are attributes, while operations such as calculating an average, displaying information, or updating a grade can be methods associated with that object.

Student objectOne represented entityAttributesName, ID, gradesMethodsOperations on student datacontainsprovidesoperates on
What data does an object contain, what functionality operates on that data, and how are they connected within one unit?

Objects Working Together

Object-oriented design is especially useful when a program needs to model real-world entities or create multiple similar things that interact with each other. Each object represents one of those things, carries the data it cares about, and offers functionality that operates on that data. The program can then be understood as interactions among objects rather than only as a sequence of procedures.

Student AStudent data and methodsStudent recordShared program contextObject methodHandles related dataStudent BStudent data and methodsinteracts withinteracts withdelegates related operation
How do objects interact and pass responsibility between one another compared with a procedure-oriented sequence of function calls?
Key point

The value of this organization grows when the program contains multiple similar entities and those entities need to interact. Object-oriented programming gives the design a way to represent each entity as an object rather than treating all of its data as separate values passed among unrelated functions.

Choosing an Organization

ConsiderationProcedure-oriented organizationObject-oriented organization
Main focusActions the program performsThings that exist and how they interact
Primary unitReusable procedures or functionsObjects
Data relationshipData is separate and flows between functionsData and related functionality are bundled
Useful situationA program can be naturally described as transformations performed by reusable functionsA program models real-world entities or multiple similar things that interact
Python supportPython can be organized entirely with pure functionsPython supports objects and also allows object-oriented and procedure-oriented styles to be mixed

The two paradigms emphasize different ways of organizing the same program.

Object-oriented design is not automatically better. Choose it when the problem is naturally described in terms of entities, their data, their related operations, and interactions among multiple similar things. A procedure-oriented design may be a suitable choice when the program is naturally organized as reusable functions that receive, transform, and return data. These approaches can also be combined in Python.

Python Supports Both Styles

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, or mix the two approaches according to the needs of the problem.

Key point

Learning object-oriented programming in Python does not mean abandoning functions. It means learning another way to group responsibility, then choosing or combining the available styles deliberately.

Mistakes in Choosing a Paradigm

  • Common mistake

    Treating object-oriented programming as merely putting functions inside a container.

    What it looks like

    A design may mention an object but still organize the program entirely around unrelated actions and separately passed data.

    Why it fails

    The central idea is that an object combines the data it cares about with the methods that operate on that data.

    Do this instead

    Ask which entity the data belongs to and which related operations should be organized with that entity.

  • Common mistake

    Assuming procedure-oriented programming cannot be reusable.

    What it looks like

    A function such as calculate_average can be reused with different grade data.

    Why it fails

    Procedure-oriented programming explicitly organizes code around reusable procedures or functions.

    Do this instead

    Distinguish reuse from organization. Both styles can support reusable functionality; they group that functionality differently.

  • Common mistake

    Assuming Python requires one paradigm for every program.

    What it looks like

    Rejecting a function-based part of a program because another part uses objects.

    Why it fails

    Python supports both paradigms and allows them to be chosen or mixed according to the problem's needs.

    Do this instead

    Use procedure-oriented, object-oriented, or mixed organization where each is appropriate.

  • Common mistake

    Choosing objects without a problem that benefits from them.

    What it looks like

    Representing every small transformation as an interacting real-world entity when the task is naturally a sequence of reusable data-processing functions.

    Why it fails

    Object-oriented organization is useful when modeling entities or multiple similar things that interact, but it is not automatically the best organization.

    Do this instead

    Compare the problem's natural structure with the strengths of each paradigm before choosing.

Practice the Distinction

MEDIUM

A program tracks several students. For each student, it stores a name, an ID, and grades. It must calculate averages, display information, and update grades. Describe how you would organize this program procedurally and how you would organize it with objects. Then explain which design better represents the students as entities that may interact, and why.

Hints
  • For the procedure-oriented design, identify the reusable functions and the data each function receives.
  • For the object-oriented design, identify the attributes and methods that belong with one student.
  • Use the presence of multiple similar students and possible interactions when explaining your choice.

What do you think happens?

Before reading the explanation, predict which organization keeps a student's name, ID, grades, and related operations together: a collection of separate procedures or an object representing the student.

  • A collection of separate procedures

  • An object representing the student

  • Both organize the data in exactly the same way

Reveal answer
Answer:

An object representing the student

Object-oriented programming bundles an object's attributes and the methods that operate on them into one unit. In a procedure-oriented design, the data remains separate from the functions and flows between them.

Key Takeaways

  1. 1
    Procedure-oriented programming organizes a program around reusable functions, with data flowing between those functions.
  2. 2
    Object-oriented programming organizes a program around objects that combine attributes and methods.
  3. 3
    Objects are useful for modeling real-world entities and creating multiple similar things that interact.
  4. 4
    Python supports pure functions, object-oriented organization, and combinations of both approaches.
  5. 5
    The best choice depends on whether the problem is naturally centered on actions and data transformations or on entities and their interactions.

At a glance

  • Procedure-oriented programming focuses on reusable actions and the flow of data between functions.
  • Object-oriented programming focuses on entities represented as objects, each combining data with related functionality.
  • Objects are particularly useful when a program models real-world entities or multiple similar things that interact.
  • Python supports both paradigms and allows programmers to mix them according to a problem's needs.

More concepts starting with O