Concepts / Object-oriented programming

Object-oriented programming

A programming paradigm where the program is constructed around objects that integrate data and functionality.

  • CORE CONCEPT
  • Programming

Two ways to organize a program

When you write a program, you face a fundamental choice about how to structure your code. Do you build it as a collection of independent functions that transform data, or do you bundle data and the operations on that data into self-contained units? These two approaches—procedure-oriented and object-oriented programming—represent different philosophies about what a program is and how it should be organized. Python supports both, which gives you flexibility but also means you need to understand when each approach makes sense.

Procedure-oriented programming

In procedure-oriented programming, a program is built primarily from procedures (functions) that are treated as reusable pieces of the program. Data and the functions that operate on that data are kept separate. You write a function to calculate something, pass it data as arguments, and it returns a result. Another function might operate on the same data in a different way. The functions are independent units, and the data flows through them.

Imagine you are managing a bank account. In a procedure-oriented approach, you might have a variable balance and separate functions: deposit(amount), withdraw(amount), and get_balance(). Each function takes the balance as input (or modifies it directly), does its job, and returns. The balance data and the functions that work with it are not inherently connected—they just happen to operate on the same variable.

Object-oriented programming

In object-oriented programming, a program is built from objects, each bundling together data and the functions (called methods) that operate on that data. An object is a self-contained unit that knows its own state (the data it holds) and knows how to act on that state (the methods it provides). Instead of passing data to independent functions, you ask an object to perform an action on itself.

Using the same bank account example in an object-oriented approach: you create a BankAccount object. This object has a balance (its data) and methods like deposit(), withdraw(), and get_balance(). When you call account.deposit(100), you are asking the account object itself to deposit money into its own balance. The data and the methods that work with it are bundled together as one unit.

containscontainscontainscontainsBankAccount Objectbalancedata (state)deposit()methodwithdraw()methodget_balance()method
How does an object integrate data and functionality? In OOP, an object contains both its state (data) and the operations (methods) that act on that state, forming a single cohesive unit.

Comparing the two paradigms

The core difference is organizational. In procedure-oriented programming, you think of your program as a series of operations: read data, transform it, write results. Functions are the primary unit of organization. In object-oriented programming, you think of your program as a collection of entities (objects) that each manage their own state and behavior. Objects are the primary unit of organization.

Procedure-OrientedFunctions are primaryObject-OrientedObjects are primaryData separate fromfunctionsData and methodsbundled togetherControl: pass data tofunctionsControl: ask objectto act on itself
How do these two paradigms organize code differently? What gets grouped together, and how does control flow differ?

Python's approach to OOP

Python supports both procedure-oriented and object-oriented programming. You can write pure functions and work with data separately, or you can create classes and objects. Python's object-oriented support is deliberately simpler and less verbose than larger object-oriented languages such as C++ or Java, while still being genuinely capable. This means Python does not force you into OOP—you choose when it makes sense—but it gives you the tools to use OOP effectively when you do.

Python uses dynamic typing, which means you do not declare a variable's type before using it. This affects how you write classes: you can add attributes to an object at runtime, and methods do not require explicit type declarations for their parameters or return values. This is different from C++ and Java, which use static typing and require you to declare types upfront.

PythonDynamic typingC++Static typing(required)JavaStatic typing(required)OOP optional (notrequired)OOP and proceduralmixedOOP enforced(everything is anobject)Simpler syntaxMore verboseMore verbose
Which OOP features does each language support, and where do they differ in how strictly they enforce OOP principles?

Why the difference matters

Because Python uses dynamic typing, you can write a class and add new attributes to instances of that class even after the class is defined. In C++ or Java, you must declare all attributes and their types when you define the class. This makes Python more flexible but also means you need to be more careful about understanding what attributes an object actually has at any given moment. Python's simpler syntax also means less boilerplate code—you do not need to write type declarations everywhere—but it also means you need to rely more on documentation and testing to ensure your code is correct.

When to use each paradigm

Procedure-oriented programming works well for small scripts, data processing pipelines, and situations where you are primarily transforming data through a series of steps. Object-oriented programming works well when you have entities in your problem domain that have both state and behavior—like a BankAccount, a User, a Game Character, or a Document. If your program naturally breaks down into things that have properties and actions, OOP is a good fit. If your program is primarily a sequence of transformations, procedure-oriented may be simpler.

Python lets you mix both paradigms in the same program. You might write some functions for utility operations and also define classes for your core entities. This flexibility is one of Python's strengths, but it also means you need to make intentional choices about which approach to use where.

Common misconceptions

  • OOP is always better than procedure-oriented programming

    Both paradigms have strengths and weaknesses. OOP is powerful for modeling complex entities, but it can add unnecessary complexity to simple tasks. A small utility script often benefits from a procedure-oriented approach.

    Fix: Choose the paradigm that best fits your problem. Use OOP when you have entities with state and behavior; use procedure-oriented when you are primarily transforming data.

  • In Python, you must use classes and objects

    Python supports both paradigms equally. You can write entire programs using only functions and variables, and many Python programs do exactly that.

    Fix: Use classes when they make your code clearer and more organized. Do not use them just because they exist.

  • Python's dynamic typing means you do not need to think about types

    Dynamic typing means the language does not enforce type checking for you, but types still matter. If you pass the wrong type of data to a function, your program will fail at runtime instead of at compile time.

    Fix: Be intentional about what types your functions and methods expect. Use comments or type hints to document this, and test your code thoroughly.

  • Objects in Python work the same way as in C++ or Java

    Python's dynamic typing and simpler syntax mean objects behave differently. You can add attributes to objects after they are created, and you do not need to declare types upfront.

    Fix: Learn Python's specific OOP model, not just the general concept of OOP. Understand that Python's approach is more flexible but also requires more discipline.

Practice: Recognizing paradigms

MEDIUM

For each of the following scenarios, decide whether a procedure-oriented or object-oriented approach would be more natural. Explain your reasoning. 1. A program that reads a CSV file, filters rows based on a condition, and writes the results to a new CSV file. 2. A program that manages a library: books can be checked out and returned, members have borrowing limits, and the system tracks overdue books. 3. A program that calculates statistics (mean, median, standard deviation) on a list of numbers. 4. A game where characters have health, mana, inventory, and can perform actions like attack, cast spell, or use item.

Hints
  • Think about whether the problem naturally involves entities with state and behavior.
  • Consider whether you are primarily transforming data or managing multiple related pieces of information.
  • Remember that Python lets you use both paradigms, so the question is which is more natural, not which is required.

Summary

Object-oriented programming and procedure-oriented programming represent two different ways to organize code. Procedure-oriented programming builds programs from independent functions that transform data. Object-oriented programming builds programs from objects that bundle data and the methods that operate on that data together. Python supports both paradigms, giving you flexibility to choose the approach that best fits your problem. Python's dynamic typing and simpler syntax make OOP more flexible than in languages like C++ and Java, but also require more discipline. Choose OOP when your problem naturally involves entities with state and behavior; use procedure-oriented programming for simpler data transformations and utility scripts.

Key Takeaways

  • Procedure-oriented programming organizes code around independent functions; object-oriented programming organizes code around objects that bundle data and methods together.
  • In OOP, an object is a self-contained unit that knows its own state and how to act on that state, unlike procedure-oriented code where data and functions are separate.
  • Python's dynamic typing and simpler syntax make OOP more flexible than C++ or Java, but require more intentional design and testing.
  • Python supports both paradigms equally; choose OOP when your problem involves entities with state and behavior, and procedure-oriented programming for simpler transformations.
  • Both paradigms have strengths and weaknesses; the best choice depends on your specific problem, not on which paradigm is inherently better.