HOME HTML EDITOR C JAVA PHP python

What is OOP in Python?

Object-Oriented Programming (OOP) is a programming paradigm that organizes software around objects rather than just functions and logic. An object bundles data (attributes/properties) and behavior (methods/functions) together in one unit.

1. Class vs. Object

A class is a blueprint or template (for example, a Course blueprint), while an object is a concrete instance created from that blueprint (such as a specific Python course object):

class Course:
    def __init__(self, title, lessons):
        self.title = title
        self.lessons = lessons

    def summary(self):
        return f"{self.title} ({self.lessons} lessons)"

py_course = Course("Python Basics", 25)
print(py_course.summary())  # Output: Python Basics (25 lessons)

2. Four Core Pillars of OOP

  • Encapsulation: Grouping related data and methods inside a class and controlling direct access to internal attributes.
  • Inheritance: Allowing a child class to reuse attributes and methods of an existing parent class.
  • Polymorphism: Allowing different classes to define methods with the same name that behave according to their own type.
  • Abstraction: Hiding complex internal implementation details and exposing only clean, essential interfaces.

3. Why Use OOP?

OOP makes large codebases easier to structure, reuse, test, and scale — especially when modeling real-world entities like users, orders, bank accounts, or UI components.

Tip: Almost everything in Python is an object — strings, integers, lists, functions, and even classes themselves have attributes and methods.