Table of Contents
Why Another Way of Thinking About Programs?
Up to now, you’ve written Python programs that mostly work with:
- Variables
- Functions
- Lists, dictionaries, etc.
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:
- 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)
- 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:
- Player data lives in global variables
- Behaviors are free functions that work on that data
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:
- Data (attributes):
namehealth- Behavior (methods):
take_damage- maybe
heal,attack, etc.
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:
- Object
A self-contained unit that combines data and behavior.
Example ideas: aPlayer, aCar, aBankAccount, aButtonin a user interface. - Class
A blueprint for objects.
It describes what data and behaviors the objects will have.
(You don’t need the syntax yet—just the idea that a class defines what kind of object you’re dealing with.)
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.
- Data it has:
- Owner’s name
- Balance
- Account number
- What it does:
- Deposit money
- Withdraw money
- Show current balance
In OOP, you’d create a BankAccount object that:
- Stores its own data (name, balance, etc.)
- Has its own actions (methods) to work with that data (deposit, withdraw, etc.)
Instead of separate variables and functions floating around, you have:
my_accountas one object with everything about that account inside it.
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:
Playerfor player-related stuffEnemyfor enemy behaviorGamefor game rulesBankAccount,Customer,Transactionin a banking app
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:
- Data:
speed,fuel - Behavior:
accelerate(),brake(),refuel()
Instead of passing the car’s speed and fuel into many separate functions, you ask the car object to do the work:
car.accelerate()car.brake()
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:
car1 = Car(...)car2 = Car(...)
Both follow the same structure but have different data (different colors, speeds, etc.).
OOP also makes it easier to:
- Reuse existing objects in new programs
- Extend or modify behavior without rewriting everything
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:
- Non-OOP thinking:
“First, ask for input. Then calculate the price. Then apply discount. Then print receipt.” - OOP thinking:
“I have: - a
Customerobject - a
Cartobject - a
Productobject - an
Orderobject
Each one knows how to handle its part of the work.”
You still write steps, but you express many of them as:
- “Ask this object to do something”
(call a method:cart.add_item(product),order.calculate_total())
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()"hello"is a string objecttext.upper()is calling a method that belongs to that string object
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:
- You have many entities with similar properties and behaviors
(players, enemies, vehicles, employees, products, etc.) - You want to model real-world systems
(library system, booking system, game world) - Your project is large and needs clear structure
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:
- See how to define classes (the blueprints)
- Create objects from those classes
- Add attributes (data) and methods (behavior) to your objects
- Learn how OOP ideas like encapsulation and inheritance help you build cleaner, more powerful programs
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.