Q&A
What is an abstraction in Python?
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:
- Abstraction - hiding complexity; exposing a simple interface (the "what"). Design-level.
- Encapsulation - hiding data/state; bundling data with methods and restricting direct access (the "how it's protected"). Implementation-level.
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
- Manage complexity - callers use a simple interface, not internal details.
- Enforce contracts - ABCs guarantee subclasses implement required methods.
- Enable polymorphism - interchangeable implementations behind one interface.
- Improve maintainability - internals can change without breaking callers.
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
- Forgetting to inherit from
ABC(just using@abstractmethodwithout it doesn't prevent instantiation). - Confusing abstraction with encapsulation.
- Expecting
__privateto be truly private (it's name-mangled, not enforced). - Over-engineering with ABCs where simple duck typing would suffice (un-Pythonic).
- Not implementing every abstract method in a subclass → it remains abstract and uninstantiable.
References
- Python
abcmodule: https://docs.python.org/3/library/abc.html - Python
typing.Protocol: https://docs.python.org/3/library/typing.html#typing.Protocol