KAHIBARO Bookshelves
Discord Login Register

13.1. What is object-oriented programming?

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:

  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:

  • 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):
    • name
    • health
  • 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: a Player, a Car, a BankAccount, a Button in 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_account as 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:

  • Player for player-related stuff
  • Enemy for enemy behavior
  • Game for game rules
  • BankAccount, Customer, Transaction in 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 Customer object
    • a Cart object
    • a Product object
    • an Order object

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 object
  • text.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.

Views: 0

Comments

Please login to add a comment.

Don't have an account? Register now!

Where to Move

Move chapter:

  • ☰ 1. Chapter 1: What Is Programming?
    • ☰ 1.1. What is a computer program?
      • ☰ 1.2. What is Python?
        • ☰ 1.3. Where is Python used?
          • ☰ 1.4. How programming helps solve problems
            • ☰ 1.5. Your first Python program
            • ☰ 2. Chapter 2: Setting Up Python
              • ☰ 2.1. Installing Python (Windows / macOS / Linux)
                • ☰ 2.2. Using Python in the terminal
                  • ☰ 2.3. Using an IDE (IDLE / VS Code)
                    • ☰ 2.4. Running Python scripts
                      • ☰ 2.5. Python interactive mode
                      • ☰ 3. Chapter 3: Variables and Data Types
                        • ☰ 3.1. What is a variable?
                          • ☰ 3.2. Creating variables
                            • ☰ 3.3. Numbers
                              • ☰ 3.4. Test
                                • ☰ 3.5. Boolean values
                                  • ☰ 3.6. Type checking
                                  • ☰ 4. Chapter 4: Basic Operations
                                    • ☰ 4.1. Arithmetic operations
                                      • ☰ 4.2. Working with numbers
                                        • ☰ 4.3. String operations
                                          • ☰ 4.4. Combining text and variables
                                            • ☰ 4.5. Comments in Python
                                            • ☰ 5. Chapter 5: User Input and Output
                                              • ☰ 5.1. Getting input from the user
                                                • ☰ 5.2. Converting input to numbers
                                                  • ☰ 5.3. Formatting output
                                                    • ☰ 5.4. Writing interactive programs
                                                    • ☰ 6. Chapter 6: Conditions
                                                      • ☰ 6.1. Comparison operators
                                                        • ☰ 6.2. Logical operators
                                                          • ☰ 6.3. if, elif, else
                                                            • ☰ 6.4. Nested conditions
                                                              • ☰ 6.5. Common beginner mistakes
                                                              • ☰ 7. Chapter 7: Loops
                                                                • ☰ 7.1. Why loops are useful
                                                                  • ☰ 7.2. for loops
                                                                    • ☰ 7.3. while loops
                                                                      • ☰ 7.4. Loop counters
                                                                        • ☰ 7.5. break and continue
                                                                          • ☰ 7.6. Loop exercises
                                                                          • ☰ 8. Chapter 8: Data Collections
                                                                            • ☰ 8.1. Lists
                                                                              • ☰ 8.2. Tuples
                                                                                • ☰ 8.3. Dictionaries
                                                                                  • ☰ 8.4. Sets (basics)
                                                                                    • ☰ 8.5. Choosing the right data structure
                                                                                    • ☰ 9. Chapter 9: Functions
                                                                                      • ☰ 9.1. Why functions matter
                                                                                        • ☰ 9.2. Defining functions
                                                                                          • ☰ 9.3. Parameters and arguments
                                                                                            • ☰ 9.4. Return values
                                                                                              • ☰ 9.5. Scope (local vs global)
                                                                                                • ☰ 9.6. Reusing code
                                                                                                • ☰ 10. Chapter 10: Working with Files
                                                                                                  • ☰ 10.1. What is a file?
                                                                                                    • ☰ 10.2. Reading text files
                                                                                                      • ☰ 10.3. Writing and appending files
                                                                                                        • ☰ 10.4. File paths
                                                                                                          • ☰ 10.5. Simple file-based programs
                                                                                                          • ☰ 11. Chapter 11: Errors and Debugging
                                                                                                            • ☰ 11.1. Types of errors
                                                                                                              • ☰ 11.2. Reading error messages
                                                                                                                • ☰ 11.3. Common beginner errors
                                                                                                                  • ☰ 11.4. try and except
                                                                                                                    • ☰ 11.5. Debugging strategies
                                                                                                                    • ☰ 12. Chapter 12: Using Python Libraries
                                                                                                                      • ☰ 12.1. What is a library?
                                                                                                                        • ☰ 12.2. Importing modules
                                                                                                                          • ☰ 12.3. Standard libraries
                                                                                                                            • ☰ 12.3.1. math
                                                                                                                              • ☰ 12.3.2. random
                                                                                                                                • ☰ 12.3.3. datetime
                                                                                                                                • ☰ 12.4. Installing external libraries with pip
                                                                                                                                • ☰ 13. Chapter 13: Object-Oriented Programming
                                                                                                                                  • ☰ 13.1. What is object-oriented programming?
                                                                                                                                    • ☰ 13.2. Classes and objects
                                                                                                                                      • ☰ 13.3. The `__init__` method
                                                                                                                                        • ☰ 13.4. Attributes and methods
                                                                                                                                          • ☰ 13.5. Encapsulation
                                                                                                                                            • ☰ 13.6. Inheritance (basics)
                                                                                                                                              • ☰ 13.7. Simple OOP examples
                                                                                                                                              • ☰ 14. Chapter 14: Automation with Python
                                                                                                                                                • ☰ 14.1. What is automation?
                                                                                                                                                  • ☰ 14.2. Automating simple tasks
                                                                                                                                                    • ☰ 14.3. Working with files and folders
                                                                                                                                                      • ☰ 14.4. Automating text processing
                                                                                                                                                        • ☰ 14.5. Scheduling scripts
                                                                                                                                                          • ☰ 14.6. Practical automation examples
                                                                                                                                                          • ☰ 15. Chapter 15: Web Development with Python
                                                                                                                                                            • ☰ 15.1. How the web works (basic overview)
                                                                                                                                                              • ☰ 15.2. Introduction to Flask
                                                                                                                                                                • ☰ 15.3. Creating a simple web server
                                                                                                                                                                  • ☰ 15.4. Routing and templates
                                                                                                                                                                    • ☰ 15.5. Handling user input (forms)
                                                                                                                                                                      • ☰ 15.6. Very basic APIs
                                                                                                                                                                      • ☰ 16. Chapter 16: Data Science with Python
                                                                                                                                                                        • ☰ 16.1. What is data science?
                                                                                                                                                                          • ☰ 16.2. Working with data
                                                                                                                                                                            • ☰ 16.3. Introduction to NumPy
                                                                                                                                                                              • ☰ 16.4. Introduction to pandas
                                                                                                                                                                                • ☰ 16.5. Data visualization with matplotlib
                                                                                                                                                                                  • ☰ 16.6. Simple data analysis examples
                                                                                                                                                                                  • ☰ 17. Chapter 17: Mini Projects
                                                                                                                                                                                    • ☰ 17.1. Number guessing game
                                                                                                                                                                                      • ☰ 17.2. Simple calculator
                                                                                                                                                                                        • ☰ 17.3. To-do list
                                                                                                                                                                                          • ☰ 17.4. Password generator
                                                                                                                                                                                            • ☰ 17.5. Quiz application
                                                                                                                                                                                              • ☰ 17.6. Small web or data project
                                                                                                                                                                                              • ☰ 18. Chapter 18: Next Steps
                                                                                                                                                                                                • ☰ 18.1. How to practice Python
                                                                                                                                                                                                  • ☰ 18.2. Writing clean and readable code
                                                                                                                                                                                                    • ☰ 18.3. Choosing a specialization
                                                                                                                                                                                                      • ☰ 18.3.1. Web development
                                                                                                                                                                                                        • ☰ 18.3.2. Data science
                                                                                                                                                                                                          • ☰ 18.3.3. Automation
                                                                                                                                                                                                            • ☰ 18.3.4. Software development
                                                                                                                                                                                                            • ☰ 18.4. Learning resources and communities
                                                                                                                                                                                                            • ☰ 19. Appendix
                                                                                                                                                                                                              • ☰ 19.1. Python syntax cheat sheet
                                                                                                                                                                                                                • ☰ 19.2. Common errors and solutions
                                                                                                                                                                                                                  • ☰ 19.3. Glossary of terms