Browse OOP reference
Modelling and persistence

What are the SOLID principles?

On this page

Single responsibility, open–closed, Liskov substitution, interface segregation and dependency inversion are design heuristics. Apply one to a concrete pressure. If changing a PDF header forces a tax rules test to change, separate rendering from calculation and check that each now changes independently.

Worked example

Python
from typing import Protocol

class TaxPolicy(Protocol):
    def cents(self, subtotal: int) -> int: ...

class Exempt:
    def cents(self, subtotal: int) -> int:
        return 0

def invoice_total(subtotal: int, tax: TaxPolicy) -> int:
    return subtotal + tax.cents(subtotal)

assert invoice_total(500, Exempt()) == 500

PEP 544 on structural subtyping

Explore class relationships in the OOP Playground

How does object-oriented design apply to AI engineering?What is object-oriented design?Is object-oriented programming obsolete?

All OOP questions

Search ObjectFAQ