Skip to content

Classes

Single Responsibility Principle (SRP)

Robert C. Martin writes:

A class should have only one reason to change.

"Reasons to change" are, in essence, the responsibilities managed by a class or function.

In the following example, we create an HTML element that represents a comment with the version of the document:

Bad

from importlib import metadata


class VersionCommentElement:
     """An element that renders an HTML comment with the program's version number
     """

     def get_version(self) -> str:
          """Get the package version"""
          return metadata.version("pip")

     def render(self) -> None:
          print(f'<!-- Version: {self.get_version()} -->')


VersionCommentElement().render()

This class has two responsibilities:

  • Retrieve the version number of the Python package
  • Render itself as an HTML element

Any change to one or the other carries the risk of impacting the other.

We can rewrite the class and decouple these responsibilities:

Good

from importlib import metadata


def get_version(pkg_name: str) -> str:
     """Retrieve the version of a given package"""
     return metadata.version(pkg_name)


class VersionCommentElement:
     """An element that renders an HTML comment with the program's version number
     """

     def __init__(self, version: str):
          self.version = version

     def render(self) -> None:
          print(f'<!-- Version: {self.version} -->')


VersionCommentElement(get_version("pip")).render()

The result is that the class only needs to take care of rendering itself. It receives the version text during instantiation and this text is generated by calling a separate function, get_version(). Changing the class has no impact on the other, and vice-versa, as long as the contract between them does not change, i.e. the function provides a string and the class __init__ method accepts a string.

As an added bonus, the get_version() is now reusable elsewhere.

Open/Closed Principle (OCP)

“Incorporate new features by extending the system, not by making modifications (to it)”, Uncle Bob.

Objects should be open for extension, but closed to modification. It should be possible to augment the functionality provided by an object (for example, a class) without changing its internal contracts. An object can enable this when it is designed to be extended cleanly.

In the following example, we try to implement a simple web framework that handles HTTP requests and returns responses. The View class has a single method .get() that will be called when the HTTP server will receive a GET request from a client.

View is intentionally simple and returns text/plain responses. We would also like to return HTML responses based on a template file, so we subclass it using the TemplateView class.

Bad

from dataclasses import dataclass


@dataclass
class Response:
     """An HTTP response"""

     status: int
     content_type: str
     body: str


class View:
     """A simple view that returns plain text responses"""

     def get(self, request) -> Response:
          """Handle a GET request and return a message in the response"""
          return Response(
               status=200,
               content_type='text/plain',
               body="Welcome to my web site"
          )


class TemplateView(View):
     """A view that returns HTML responses based on a template file."""

     def get(self, request) -> Response:
          """Handle a GET request and return an HTML document in the response"""
          with open("index.html") as fd:
               return Response(
                    status=200,
                    content_type='text/html',
                    body=fd.read()
               )

The TemplateView class has modified the internal behaviour of its parent class in order to enable the more advanced functionality. In doing so, it now relies on the View to not change the implementation of the .get() method, which now needs to be frozen in time. We cannot introduce, for example, some additional checks in all our View-derived classes because the behaviour is overridden in at least one subtype and we will need to update it.

Let's redesign our classes to fix this problem and let the View class be extended (not modified) cleanly:

Good

from dataclasses import dataclass


@dataclass
class Response:
     """An HTTP response"""

     status: int
     content_type: str
     body: str


class View:
     """A simple view that returns plain text responses"""

     content_type = "text/plain"

     def render_body(self) -> str:
          """Render the message body of the response"""
          return "Welcome to my web site"

     def get(self, request) -> Response:
          """Handle a GET request and return a message in the response"""
          return Response(
               status=200,
               content_type=self.content_type,
               body=self.render_body()
          )


class TemplateView(View):
     """A view that returns HTML responses based on a template file."""

     content_type = "text/html"
     template_file = "index.html"

     def render_body(self) -> str:
          """Render the message body as HTML"""
          with open(self.template_file) as fd:
               return fd.read()

Note that we did need to override the render_body() in order to change the source of the body, but this method has a single, well defined responsibility that invites subtypes to override it. It is designed to be extended by its subtypes.

Another good way to use the strengths of both object inheritance and object composition is to use Mixins .

Mixins are bare-bones classes that are meant to be used exclusively with other related classes. They are "mixed-in" with the target class using multiple inheritance, in order to change the target's behaviour.

A few rules:

  • Mixins should always inherit from object
  • Mixins always come before the target class, e.g. class Foo(MixinA, MixinB, TargetClass): ...

Also good

from dataclasses import dataclass, field
from typing import Protocol


@dataclass
class Response:
     """An HTTP response"""

     status: int
     content_type: str
     body: str
     headers: dict = field(default_factory=dict)


class View:
     """A simple view that returns plain text responses"""

     content_type = "text/plain"

     def render_body(self) -> str:
          """Render the message body of the response"""
          return "Welcome to my web site"

     def get(self, request) -> Response:
          """Handle a GET request and return a message in the response"""
          return Response(
               status=200,
               content_type=self.content_type,
               body=self.render_body()
          )


class TemplateRenderMixin:
     """A mixin class for views that render HTML documents using a template file

     Not to be used by itself!
     """
     template_file: str = ""

     def render_body(self) -> str:
          """Render the message body as HTML"""
          if not self.template_file:
               raise ValueError("The path to a template file must be given.")

          with open(self.template_file) as fd:
               return fd.read()


class ContentLengthMixin:
     """A mixin class for views that injects a Content-Length header in the
     response

     Not to be used by itself!
     """

     def get(self, request) -> Response:
          """Introspect and amend the response to inject the new header"""
          response = super().get(request)  # type: ignore
          response.headers['Content-Length'] = len(response.body)
          return response


class TemplateView(TemplateRenderMixin, ContentLengthMixin, View):
     """A view that returns HTML responses based on a template file."""

     content_type = "text/html"
     template_file = "index.html"

As you can see, Mixins make object composition easier by packaging together related functionality into a highly reusable class with a single responsibility, allowing clean decoupling. Class extension is achieved by " mixing-in" the additional classes.

The popular Django project makes heavy use of Mixins to compose its class-based views.

FIXME: re-enable typechecking for the line above once it's clear how to use typing.Protocol to make the type checker work with Mixins.

Liskov Substitution Principle (LSP)

“Functions that use pointers or references to base classes must be able to use objects of derived classes without knowing it”, Uncle Bob.

This principle is named after Barbara Liskov, who collaborated with fellow computer scientist Jeannette Wing on the seminal paper *"A behavioral notion of subtyping" (1994). A core tenet of the paper is that "a subtype (must) preserve the behaviour of the supertype methods and also all invariant and history properties of its supertype".

In essence, a function accepting a supertype should also accept all its subtypes with no modification.

Can you spot the problem with the following code?

Bad

from dataclasses import dataclass


@dataclass
class Response:
     """An HTTP response"""

     status: int
     content_type: str
     body: str


class View:
     """A simple view that returns plain text responses"""

     content_type = "text/plain"

     def render_body(self) -> str:
          """Render the message body of the response"""
          return "Welcome to my web site"

     def get(self, request) -> Response:
          """Handle a GET request and return a message in the response"""
          return Response(
               status=200,
               content_type=self.content_type,
               body=self.render_body()
          )


class TemplateView(View):
     """A view that returns HTML responses based on a template file."""

     content_type = "text/html"

     def get(self, request, template_file: str) -> Response:  # type: ignore
          """Render the message body as HTML"""
          with open(template_file) as fd:
               return Response(
                    status=200,
                    content_type=self.content_type,
                    body=fd.read()
               )


def render(view: View, request) -> Response:
     """Render a View"""
     return view.get(request)

The expectation is that render() function will be able to work with View and its subtype TemplateView, but the latter has broken compatibility by modifying the signature of the .get() method. The function will raise a TypeError exception when used with TemplateView.

If we want the render() function to work with any subtype of View, we must pay attention not to break its public-facing protocol. But how do we know what constitutes it for a given class? Type hinters like mypy will raise an error when it detects mistakes like this:

error: Signature of "get" incompatible with supertype "View"
<string>:36: note:      Superclass:
<string>:36: note:          def get(self, request: Any) -> Response
<string>:36: note:      Subclass:
<string>:36: note:          def get(self, request: Any, template_file: str) -> Response

Interface Segregation Principle (ISP)

“Keep interfaces small so that users don’t end up depending on things they don’t need.”, Uncle Bob.

Several well known object oriented programming languages, like Java and Go, have a concept called interfaces. An interface defines the public methods and properties of an object without implementing them. They are useful when we don't want to couple the signature of a function to a concrete object; we'd rather say "I don't care what object you give me, as long as it has certain methods and attributes I expect to make use of".

Python does not have interfaces. We have Abstract Base Classes instead, which are a little different, but can serve the same purpose.

Good

from abc import ABCMeta, abstractmethod


# Define the Abstract Class for a generic Greeter object
class Greeter(metaclass=ABCMeta):
     """An object that can perform a greeting action."""

     @staticmethod
     @abstractmethod
     def greet(name: str) -> None:
          """Display a greeting for the user with the given name"""


class FriendlyActor(Greeter):
     """An actor that greets the user with a friendly salutation"""

     @staticmethod
     def greet(name: str) -> None:
          """Greet a person by name"""
          print(f"Hello {name}!")


def welcome_user(user_name: str, actor: Greeter):
     """Welcome a user with a given name using the provided actor"""
     actor.greet(user_name)


welcome_user("Barbara", FriendlyActor())

Now imagine the following scenario: we have a certain number of PDF documents that we author and want to serve to our web site visitors. We are using a Python web framework and we might be tempted to design a class to manage these documents, so we go ahead and design a comprehensive abstract base class for our document.

Error

import abc


class Persistable(metaclass=abc.ABCMeta):
     """Serialize a file to data and back"""

     @property
     @abc.abstractmethod
     def data(self) -> bytes:
          """The raw data of the file"""

     @classmethod
     @abc.abstractmethod
     def load(cls, name: str):
          """Load the file from disk"""

     @abc.abstractmethod
     def save(self) -> None:
          """Save the file to disk"""


# We just want to serve the documents, so our concrete PDF document
# implementation just needs to implement the `.load()` method and have
# a public attribute named `data`.

class PDFDocument(Persistable):
     """A PDF document"""

     @property
     def data(self) -> bytes:
          """The raw bytes of the PDF document"""
          ...  # Code goes here - omitted for brevity

     @classmethod
     def load(cls, name: str):
          """Load the file from the local filesystem"""
          ...  # Code goes here - omitted for brevity


def view(request):
     """A web view that handles a GET request for a document"""
     requested_name = request.qs['name']  # We want to validate this!
     return PDFDocument.load(requested_name).data

But we can't! If we don't implement the .save() method, an exception will be raised:

Can't instantiate abstract class PDFDocument with abstract method save.

That's annoying. We don't really need to implement .save() here. We could implement a dummy method that does nothing or raises NotImplementedError, but that's useless code that we will need to maintain.

At the same time, if we remove .save() from the abstract class now we will need to add it back when we will later implement a way for users to submit their documents, bringing us back to the same situation as before.

The problem is that we have written an interface that has features we don't need right now as we are not using them.

The solution is to decompose the interface into smaller and composable interfaces that segregate each feature.

Good

import abc


class DataCarrier(metaclass=abc.ABCMeta):
     """Carries a data payload"""

     @property
     def data(self):
          ...


class Loadable(DataCarrier):
     """Can load data from storage by name"""

     @classmethod
     @abc.abstractmethod
     def load(cls, name: str):
          ...


class Saveable(DataCarrier):
     """Can save data to storage"""

     @abc.abstractmethod
     def save(self) -> None:
          ...


class PDFDocument(Loadable):
     """A PDF document"""

     @property
     def data(self) -> bytes:
          """The raw bytes of the PDF document"""
          ...  # Code goes here - omitted for brevity

     @classmethod
     def load(cls, name: str) -> None:
          """Load the file from the local filesystem"""
          ...  # Code goes here - omitted for brevity


def view(request):
     """A web view that handles a GET request for a document"""
     requested_name = request.qs['name']  # We want to validate this!
     return PDFDocument.load(requested_name).data

Dependency Inversion Principle (DIP)

“Depend upon abstractions, not concrete details”, Uncle Bob.

Imagine we wanted to write a web view that returns an HTTP response that streams rows of a CSV file we create on the fly. We want to use the CSV writer that is provided by the standard library.

Bad

import csv
from io import StringIO


class StreamingHttpResponse:
     """A streaming HTTP response"""
     ...  # implementation code goes here


def some_view(request):
     rows = (
          ['First row', 'Foo', 'Bar', 'Baz'],
          ['Second row', 'A', 'B', 'C', '"Testing"', "Here's a quote"]
     )

     # Define a generator to stream data directly to the client
     def stream():
          buffer_ = StringIO()
          writer = csv.writer(buffer_, delimiter=';', quotechar='"')
          for row in rows:
               writer.writerow(row)
               buffer_.seek(0)
               data = buffer_.read()
               buffer_.seek(0)
               buffer_.truncate()
               yield data

     # Create the streaming response  object with the appropriate CSV header.
     response = StreamingHttpResponse(stream(), content_type='text/csv')
     response[
          'Content-Disposition'] = 'attachment; filename="somefilename.csv"'

     return response

Our first implementation works around the CSV's writer interface by manipulating a StringIO object (which is file-like) and performing several low level operations in order to farm out the rows from the writer. It's a lot of work and not very elegant.

A better way is to leverage the fact that the writer just needs an object with a .write() method to do our bidding. Why not pass it a dummy object that immediately returns the newly assembled row, so that the StreamingHttpResponse class can immediate stream it back to the client?

Good

import csv


class Echo:
     """An object that implements just the write method of the file-like
     interface.
     """

     def write(self, value):
          """Write the value by returning it, instead of storing in a buffer."""
          return value


def some_streaming_csv_view(request):
     """A view that streams a large CSV file."""
     rows = (
          ['First row', 'Foo', 'Bar', 'Baz'],
          ['Second row', 'A', 'B', 'C', '"Testing"', "Here's a quote"]
     )
     writer = csv.writer(Echo(), delimiter=';', quotechar='"')
     return StreamingHttpResponse(
          (writer.writerow(row) for row in rows),
          content_type="text/csv",
          headers={
               'Content-Disposition': 'attachment; filename="somefilename.csv"'},
     )

Much better, and it works like a charm! The reason it's superior to the previous implementation should be obvious: less code (and more performant) to achieve the same result. We decided to leverage the fact that the writer class depends on the .write() abstraction of the object it receives, without caring about the low level, concrete details of what the method actually does.

This example was taken from a submission made to the Django documentation by this author.

Comments