Interfaces

Intermediate Object-Oriented Programming in Python

Jake Roach

Data Engineer

Interfaces

A special kind of class made up of only abstract methods that create a contract with the classes that implement the interface

  • Must define abstract methods
  • Skeleton of method definitions
  • Parameters do not need to match
  • Formal or informal

$$

$$

Let's start with informal interfaces!

A diagram of a Python interface with three abstract methods.

Intermediate Object-Oriented Programming in Python

Informal interfaces with duck typing

class Course:
    def assign_homework(self, assignment_number, due_date):
        # Typically, the pass keyword will be used when creating an interface
        pass  

    def grade_assignment(self, assignment_number):
        pass
  • Duck typing
  • assign_homework() and grade_assignment() abstract methods
Intermediate Object-Oriented Programming in Python

Informal interfaces with duck typing

class ProgrammingCourse:
    def __init__(self, course_name):
        self.course_name = course_name

    def assign_homework(self, due_date):
        # Some implementation of assign_homework here...

    def grade_assignment(self, assignment_number):
        # Some implementation of grade_assignment here...
Intermediate Object-Oriented Programming in Python

Formal interfaces with ABC, @abstractmethod

from abc import ABC, abstractmethod

class Course(ABC):    
    @abstractmethod
    def assign_homework(self, assignment_number, due_date):
        pass

    @abstractmethod
    def grade_assignment(self, assignment_number):
        pass

How are formal interfaces different than abstract base classes?

Intermediate Object-Oriented Programming in Python

Interfaces vs. Abstract Base Classes

Interfaces

  • Define a "contract" with classes that implement it
  • "Must-do"
  • Formal and informal
  • Only contain abstract methods (no concrete methods)
  • Nearly identical classes

Abstract base classes

  • Create a "blueprint" for classes with common characteristics
  • "Is-a"
  • Typically only formal
  • Mix of abstract and concrete methods
  • Classes that look and feel similar
Intermediate Object-Oriented Programming in Python

Implementing a formal interface

class ProgrammingCourse(Course):
    def __init__(self, course_name):
        self.course_name = course_name

    def assign_homework(self, due_date):
        # Some implementation of assign_homework here...

    def grade_assignment(self, assignment_number):
        # Some implementation of grade_assignment here...

intermediate_oop = ProgrammingCourse("Intermediate Object-Oriented Programming")
Intermediate Object-Oriented Programming in Python

Failing to implement a formal interface

class ProgrammingCourse(Course):
    def __init__(self, course_name):
        self.course_name = course_name

    def assign_homework(self, due_date):
        # Some implementation of assign_homework here...

intermediate_oop = ProgrammingCourse("Intermediate Object-Oriented Programming")
 ...
    intermediate_oop = ProgrammingCourse("Intermediate Object-Oriented Programming")
TypeError: Can't instantiate abstract class ProgrammingCourse with abstract method
    grade_assignment
Intermediate Object-Oriented Programming in Python

Let's practice!

Intermediate Object-Oriented Programming in Python

Preparing Video For Download...