AlgoInsight

Q&A

What is an abstraction in Python?

By AlgoInsight Team2026-06-07ooppython

Overview

Abstraction lets you work with high-level concepts without worrying about internals - you call vehicle.start() without knowing the engine details. It reduces complexity, defines clear contracts, and lets implementations vary behind a stable interface. Python supports it through abstract base classes, duck typing, and protocols.

graph TD
    A[Abstraction] --> B[Define a contract: abstract methods]
    A --> C[Hide implementation behind the interface]
    B --> D[ABCs: abc.ABC + @abstractmethod]
    D --> E[Subclasses MUST implement abstract methods]
    D --> F[Cannot instantiate the abstract class]
    style D fill:#1e1b4b,color:#fff

Abstract Base Classes (the abc module)

Declare an abstract class by inheriting from ABC and marking required methods with @abstractmethod:

from abc import ABC, abstractmethod

class Shape(ABC):
    @abstractmethod
    def area(self):
        ...                      # subclasses must define this

    @abstractmethod
    def perimeter(self):
        ...

    def describe(self):          # concrete method shared by all shapes
        return f"Area={self.area()}, Perimeter={self.perimeter()}"

class Circle(Shape):
    def __init__(self, r): self.r = r
    def area(self):      return 3.14159 * self.r ** 2
    def perimeter(self): return 2 * 3.14159 * self.r

class Rectangle(Shape):
    def __init__(self, w, h): self.w, self.h = w, h
    def area(self):      return self.w * self.h
    def perimeter(self): return 2 * (self.w + self.h)
c = Circle(5)
print(c.describe())     # Area=78.53975, Perimeter=31.4159

s = Shape()             # ❌ TypeError: Can't instantiate abstract class Shape
                        #    with abstract methods area, perimeter

Two enforcement effects: you can't instantiate Shape, and any subclass that doesn't implement all abstract methods also can't be instantiated. This guarantees the contract.

Abstraction vs Encapsulation

They're related but distinct:

Python's encapsulation uses naming conventions: _protected (convention) and __private (name-mangled). Abstraction is about the conceptual interface; encapsulation is about protecting internals.

Abstract Properties

You can require properties too:

class Animal(ABC):
    @property
    @abstractmethod
    def sound(self):
        ...

class Dog(Animal):
    @property
    def sound(self):
        return "Woof"

Duck Typing and Protocols (Pythonic abstraction)

Python doesn't always need formal ABCs. Duck typing ("if it walks like a duck…") provides informal abstraction - code works with any object that has the needed methods:

def total_area(shapes):
    return sum(s.area() for s in shapes)   # any object with .area() works

ABCs add enforcement and clear contracts when you want them. Modern Python also offers typing.Protocol for structural typing - defining an interface that any matching object satisfies without explicit inheritance:

from typing import Protocol
class HasArea(Protocol):
    def area(self) -> float: ...

Why Use Abstraction

Why This Matters in Interviews

Python OOP interviews use abstraction to test design understanding. Key points: define abstract classes with abc.ABC + @abstractmethod, the enforcement (can't instantiate the abstract class or incomplete subclasses), the abstraction vs encapsulation distinction (hiding complexity vs hiding data), and Python's flexible alternatives - duck typing and typing.Protocol for structural interfaces. Knowing concrete methods can live alongside abstract ones in an ABC shows depth.

Common Pitfalls

References

More answers