Concepts / Object-Oriented Programming Basics

Object-Oriented Programming Basics

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

  • Programming

From Actions to Things

When planning a program, you can begin with the actions it must perform or with the things that exist in the problem. Procedure-oriented programming begins with reusable functions and asks what actions the program performs. Object-oriented programming begins with objects and asks what things exist, what data they care about, and how they interact.

The central difference is organization: procedure-oriented programs center on reusable functions, while object-oriented programs center on objects that combine data and functionality.

Two Program Structures

In procedure-oriented programming, data is separate from the functions that process it. A function can take data as input, transform it, and return a result. Data therefore flows between functions, and the program is understood as a collection of reusable procedures. For student information, separate functions might calculate an average from grades, print a student's name and ID, or update a grades collection.

In object-oriented programming, related data and the functions that operate on that data are bundled within an object. The data is called attributes, and the functions are called methods. Instead of treating a student's information as data passed separately among functions, the program can treat a student as an object with attributes such as name, ID, and grades and methods that operate on that information.

passed topassed topassed tocontainscontainsStudent dataname, ID, gradescalculate_averagefunctionStudentobjectAttributesname, ID, gradesprint_student_infofunctionMethodsstudent operationsupdate_gradefunction
What changes when the same kind of program is organized around reusable functions instead of objects that combine data and functionality?
QuestionProcedure-oriented viewObject-oriented view
What is central?Reusable procedures or functionsObjects
Where is data?Separate from functionsBundled with related functionality
What is the program focused on?Actions the program performsThings that exist and how they interact

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

Inside a Cohesive Object

A Student as a Program Object

Organize a student's name, ID, and grades together with operations that use or change that information.

Identify the data: The student's name, ID, and grades are the information the student object cares about. In object-oriented terminology, these are attributes.

Identify the operations: Calculating an average, displaying student information, and updating a grade are operations related to the student's data. In object-oriented terminology, these are methods.

Bundle related parts: The attributes and methods are treated as parts of one student object rather than as unrelated data and separate functions.

Create similar things: The same object-centered idea can be used for multiple student objects, each representing its own student information and related operations.

An object combines the data it cares about with the functionality that operates on that data, making the relationship between them explicit.

hashashasprovidesprovidesprovidesStudentobjectnameattributecalculate averagemethodIDattributedisplay informationmethodgradesattributeupdate grademethod
What data does an object contain, what functions operate on that data, and how are they connected?

The benefit of bundling is conceptual cohesion: information and the functionality related to that information are considered together.

When Structure Matters

Neither paradigm is presented as universally required. Procedure-oriented organization can be suitable when the main idea is a sequence of reusable actions that receive data and return results. Object-oriented organization becomes useful when the program is naturally described in terms of entities, the data associated with those entities, and the functionality that operates on it.

Objects are especially useful for modeling real-world entities and for creating multiple similar things that interact with each other. In the student example, several student objects could represent several students, each with its own information and related operations.

data flows tocan be repeated asStudent dataname, ID, gradesStudent objectdata and functionalitySeparate functionsreceive dataStudent objectsmultiple similar things
How does program structure change as related data and behaviors become more connected or need to be reused?

Python's Flexible Model

Python fully supports procedure-oriented programming: you can write independent functions that accept data, process it, and return results. Python also supports object-oriented programming, in which objects contain attributes and methods. Because Python supports both paradigms, a program can use a procedure-oriented style, an object-oriented style, or a mixture of the two.

can containcan containhashasPython programsupports both paradigmsStudentobjectgradesattributecalculate averagemethodReusable functionprocedure-oriented option
How do Python objects, attributes, and methods relate to one another in a simple program?

Start by identifying the program's main data and actions. If the actions are the clearest organizing idea, reusable functions may fit naturally. If related data and actions belong together or several similar entities need to interact, consider an object-centered design. In Python, you are allowed to combine these approaches.

Mistakes Beginners Make

  • Treating object-oriented programming as merely a different name for using functions

    The defining distinction in this topic is that an object bundles its data, called attributes, with the functions that operate on that data, called methods.

    Fix: Ask which object owns the related data and functionality, rather than only counting how many functions the program has.

  • Assuming procedure-oriented programming cannot be reusable

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

    Fix: Recognize that both paradigms can organize reusable parts; they organize those parts around different central units.

  • Assuming Python requires every program to use objects

    Python fully supports procedure-oriented programming and allows programmers to choose or mix paradigms based on the problem's needs.

    Fix: Choose the organization that makes the problem clearest, including a mixture when appropriate.

  • Confusing an object's data with its functionality

    Attributes are the data an object cares about; methods are the functions that operate on that data.

    Fix: Classify stored information as attributes and related operations as methods.

Check Your Design Choice

MEDIUM

Imagine a program that works with several students. Each student has a name, ID, and grades. The program must calculate averages, display information, and update grades. Explain how you would organize this program procedurally and how you would organize it with objects. Then state which parts would be attributes and which would be methods in the object-oriented version.

Hints
  • For the procedure-oriented version, begin with the reusable actions and identify what data each function receives.
  • For the object-oriented version, begin with the student as an entity and group its data with related operations.
  • Remember that attributes are data and methods are functions that operate on that data.

What do you think happens?

A Python program needs reusable functions for one part of its work and objects for another part. Must the entire program use only one paradigm?

  • Yes, Python requires one consistent paradigm
  • No, Python allows paradigms to be chosen or mixed based on the problem
  • Only object-oriented programming can be mixed with functions
Reveal answer

Answer: No, Python allows paradigms to be chosen or mixed based on the problem.

Python supports both procedure-oriented and object-oriented programming, so the approaches can be selected or combined according to the problem's needs.

Key Takeaways

  1. Procedure-oriented programming organizes code around reusable functions, with data flowing between those functions.
  2. Object-oriented programming organizes code around objects that combine attributes and methods.
  3. Objects can help model real-world entities and create multiple similar things that interact.
  4. Python supports procedure-oriented programming, object-oriented programming, and mixtures of both.
  5. The right choice depends on the problem's needs and on whether actions or related entities provide the clearest organizing idea.

Key Takeaways

  • Procedure-oriented programming focuses on reusable functions and the actions a program performs.
  • Object-oriented programming focuses on objects that bundle data as attributes with related functions as methods.
  • Objects are useful for modeling entities and creating multiple similar things that interact.
  • Python supports both paradigms and lets programmers choose or combine them according to the problem.