← Back to Blog

When Functions Aren't Enough

The problem...

You've built three projects. Each one works. Each one has functions that add data, filter data, display data, export data.

But look at the Calories Tracker. add_food_item() writes to a file and calls load_from_txt(). load_from_txt() populates food_dict. last_days_details() reads from food_dict. Everything is connected — but loosely. Invisibly. If you rename food_dict, you have to find every function that touches it. If you want a second tracker for a different user, you duplicate everything.

The code works. But it doesn't scale. And it doesn't tell you, just by reading it, what belongs together.

The idea!

Object-Oriented Programming is a way of organizing code around things — objects — rather than around actions — functions.

Instead of a dictionary and a collection of functions that operate on it, you create one structure that holds both the data and the functions together. That structure is called a class. Each individual instance of it is an object.

Making it real

Think about the Safe Paws shelter. In the procedural version, an animal is a list:

paws_dict[1] = ["Lassie", "dog", "medium", "healthy", 4, "available"]

To get Lassie's name, you write paws_dict[1][0]. To get her status, paws_dict[1][5]. You have to remember what each index means.

In OOP, an animal is an object:

lassie = Animal("Lassie", "dog", "medium", "healthy", 4)
print(lassie.name)      # Lassie
print(lassie.status)    # available

The data has names. The structure is self-documenting. And the functions that operate on the animal — adopt, edit health, display — live inside the object itself.

Why OOP exists

As programs grow, procedural code becomes harder to manage. Functions multiply. Global variables accumulate. The connections between them become implicit — you have to read everything to understand anything.

OOP exists to solve exactly this. It gives you:

  • Structure — data and behavior belong together
  • Reusability — create as many objects as you need from the same class
  • Readability — code that describes what it models, not just what it does
  • Scalability — add features without breaking what already works

What changes

The logic doesn't change. Loops, conditions, file handling, dictionaries — all of it stays. What changes is how you organize it. Instead of:

food_dict = {}

def add_food_item():
    ...

def load_from_txt():
    ...

You write:

class CalorieTracker:
    def __init__(self):
        self.food_dict = {}

    def add_food_item(self):
        ...

    def load_from_txt(self):
        ...

Same functions. Same data. One structure that holds them together and makes their relationship explicit.

The mindset shift

Stop thinking: "What actions does my program perform?"

Start thinking: "What things does my program model — and what can each thing do?"

What you should understand now

  • OOP organizes code around objects — things — rather than functions — actions
  • A class is a blueprint. An object is an instance of that blueprint
  • Data and behavior belong together inside a class
  • The logic you already know doesn't change — the organization does
  • OOP makes code more readable, reusable, and scalable as programs grow
[ login to bookmark ] // copied! 34 views · 2 min
← prev The Beginning of Infinite next → Classes and Objects: The Blueprint and the Thing
// 0 comments
// No comments yet. Be the first.
// leave a comment

// Your comment will appear after approval.