Skip to content

06. Object-Oriented Programming (OOP) in Python

Overview

Object-Oriented Programming (OOP) in Python is a programming paradigm that organizes code around objects, which encapsulate data and behavior together. Python’s OOP model supports modular design, code reuse, and abstraction through classes, objects, inheritance, and polymorphism. It is widely used in application development, libraries, frameworks, and large-scale systems where maintainability and extensibility are critical.

Core OOP Concepts

Class and Object

A class defines a blueprint for creating objects, while an object is an instance of a class.

class User:
    def __init__(self, name):
        self.name = name

user = User("Alice")

In this structure: (1) User is the class definition (2) user is an object created from the class (3) self refers to the current object instance

Encapsulation

Encapsulation groups data and related behavior within a class and controls access to internal state.

class Account:
    def __init__(self, balance):
        self._balance = balance

    def get_balance(self):
        return self._balance

Python uses naming conventions rather than strict access modifiers: (1) Public attributes have no leading underscore (2) Protected attributes use a single underscore (3) Name-mangled attributes use double underscores

Note

Encapsulation in Python Python relies on developer discipline and conventions rather than enforced access control.

Class Structure and Syntax

Instance and Class Attributes

Attributes can belong to either an instance or the class itself.

class Service:
    timeout = 30

    def __init__(self, name):
        self.name = name

(1) timeout is a class attribute shared by all instances (2) name is an instance attribute unique to each object

Instance Methods

Instance methods operate on object state and must accept self as the first parameter.

class Logger:
    def log(self, message):
        print(message)

Inheritance and Composition

Inheritance Model

Inheritance allows a class to reuse and extend behavior from another class.

class Animal:
    def speak(self):
        pass

class Dog(Animal):
    def speak(self):
        return "bark"

Rules governing inheritance: (1) Subclasses inherit all public and protected members (2) Methods can be overridden to customize behavior (3) Multiple inheritance is supported but should be used cautiously

Warning

Inheritance complexity Deep or multiple inheritance hierarchies can reduce readability and complicate debugging.

Composition

Composition builds complex behavior by combining objects rather than inheriting from them.

class Engine:
    pass

class Car:
    def __init__(self):
        self.engine = Engine()

Composition is often preferred for flexibility and clearer responsibility boundaries.

Polymorphism and Dynamic Typing

Polymorphic Behavior

Polymorphism allows different objects to respond to the same method call in different ways.

def make_sound(animal):
    return animal.speak()

Python supports polymorphism through: (1) Duck typing (2) Method overriding (3) Common interfaces defined by convention

Abstract Base Classes

Abstract Base Classes define required methods for subclasses.

from abc import ABC, abstractmethod

class Storage(ABC):
    @abstractmethod
    def save(self, data):
        pass

This enforces a contract without providing implementation.

Execution Flow

Object Creation Lifecycle

The lifecycle of object creation follows a defined sequence.

(1) Class definition is loaded and evaluated (2) Object instantiation is requested (3) Memory is allocated for the new object (4) The __init__ method initializes instance state (5) The object becomes available for use

Best Practices and Constraints

Design Guidelines

(1) Prefer composition over inheritance for extensibility (2) Keep classes focused on a single responsibility (3) Avoid excessive use of mutable shared state (4) Use abstract base classes to define stable interfaces

Tip

Client-facing design checklist (1) Identify domain concepts that map naturally to objects (2) Define clear class responsibilities and boundaries (3) Document public methods and expected behavior

Limitations

(1) Python does not enforce strict access control (2) Runtime errors may occur if interfaces are not respected (3) Overuse of OOP patterns can add unnecessary complexity

Applicability and Scope

Python OOP is general-purpose and applies across: (1) Web applications and APIs (2) Data processing and automation (3) Libraries and SDK development (4) Testing and mocking frameworks

It is supported in all standard Python runtime environments without additional dependencies.

Reference