Kahibaro
Discord Login Register

What is object-oriented programming?

Why Another Way of Thinking About Programs?

Up to now, you’ve written Python programs that mostly work with:

This style is often called procedural programming: you write steps (procedures) that the computer follows.

Object-oriented programming (OOP) is a different way of organizing your code and thinking about problems. Instead of focusing on steps first, OOP focuses on things (objects) and how they behave.

You don’t need to know how to define classes or objects yet (that’s coming in later sections). Here, you just need to understand what OOP is and why it exists.

The Core Idea of OOP

Object-oriented programming is based on two main ideas:

  1. Objects represent real or imaginary “things”
    An object bundles together:
    • Data about the thing (for example: a car’s color, brand, speed)
    • Behavior of the thing (for example: start, stop, accelerate)
  2. Programs are built as collections of these objects
    Objects “work together” by sending messages to each other (in Python: calling each other’s methods).

So, instead of having one big list of steps, you have many small, self-contained objects that each handle their own responsibilities.

Comparing Two Styles: Without OOP vs With OOP

Imagine you’re writing a simple game with a player that has a name, health, and can take damage.

Without thinking in OOP terms

You might write code like:

player_name = "Alex"
player_health = 100
def take_damage(health, amount):
    return health - amount
player_health = take_damage(player_health, 20)
print(player_name, "now has", player_health, "health")

Everything is separate:

Thinking in OOP terms

In OOP, you’d think: “The player is a thing. It has data and can do actions.”

Conceptually, you imagine something like:

You think of a player object that carries its own data and knows how to work with it.

(You’ll learn how to actually define this with classes in the next sections; here we’re just focusing on the way of thinking.)

Key Concepts at a High Level

You’ll study these terms in detail in later sections, but OOP is built around a few central ideas:

OOP says:

“Bundle related data and functions together into objects, based on what they are and what they do.”

A Real-World Analogy

Think of something from everyday life: a bank account.

In OOP, you’d create a BankAccount object that:

Instead of separate variables and functions floating around, you have:

Why Object-Oriented Programming Is Useful

OOP is not “better” in every situation, but it can be very helpful as programs grow larger. Here are some broad benefits.

1. Organizing complex code

When your program is small, a few functions and variables are enough.
When it becomes large (hundreds or thousands of lines), it’s easy to get lost.

OOP helps you organize code into logical pieces:

Each piece is its own object (and based on its own class), so you know where to put new code and where to look when something breaks.

2. Grouping related data and behavior

Objects keep data and the functions that work on that data together.

For example, a Car object might have:

Instead of passing the car’s speed and fuel into many separate functions, you ask the car object to do the work:

This often makes code easier to read and reason about.

3. Reusing and extending code

Once you define a class (a blueprint), you can create many objects from it.

For example:

Both follow the same structure but have different data (different colors, speeds, etc.).

OOP also makes it easier to:

You’ll see more about reuse and extension when you learn about inheritance later in this chapter.

Thinking in Terms of “Things” Instead of “Steps”

Here’s another way to see the difference in mindset:

Each one knows how to handle its part of the work.”

You still write steps, but you express many of them as:

Where OOP Shows Up in Python

You’ve already used objects without realizing it.

When you wrote:

text = "hello"
length = len(text)
upper_text = text.upper()

Even built-in types like str, list, and dict are implemented using OOP concepts. You’re now going to learn how to define your own kinds of objects.

When Is OOP a Good Fit?

OOP is especially helpful when:

For very small, one-off scripts, OOP may be more structure than you need. For bigger programs, it can make the code much more manageable.

What You Will Learn Next

In the rest of this chapter, you will:

For now, remember this simple summary:

Object-oriented programming is a way of building programs around objects—self-contained units that combine data and behavior—to make code easier to organize, understand, and reuse.

Views: 13

Comments

Please login to add a comment.

Don't have an account? Register now!