4.12. Interfaces

4.12.1. What it is?

  • Python don't have interfaces

  • Cannot instantiate

  • Inheriting class must implement all methods

  • Only method declaration

interface
implement
Listing 4.87. Interfaces
from datetime import timedelta


class CacheInterface:
    timeout: timedelta

    def get(self, key: str) -> str:
        raise NotImplementedError

    def set(self, key: str, value: str) -> None:
        raise NotImplementedError

    def is_valid(self, key: str) -> bool:
        raise NotImplementedError

4.12.2. When use it?

4.12.3. Examples

Listing 4.88. Interfaces
from datetime import timedelta


class Cache:
    timeout: timedelta

    def get(self, key: str) -> str:
        raise NotImplementedError

    def set(self, key: str, value: str) -> None:
        raise NotImplementedError

    def is_valid(self, key: str) -> bool:
        raise NotImplementedError


class CacheDatabase(Cache):
    timeout: timedelta

    def is_valid(self, key: str) -> bool:
        ...

    def get(self, key: str) -> str:
        ...

    def set(self, key: str, value: str) -> None:
        ...


class CacheRAM(Cache):
    timeout: timedelta

    def is_valid(self, key: str) -> bool:
        ...

    def get(self, key: str) -> str:
        ...

    def set(self, key: str, value: str) -> None:
        ...


class CacheFilesystem(Cache):
    timeout: timedelta

    def is_valid(self, key: str) -> bool:
        ...

    def get(self, key: str) -> str:
        ...

    def set(self, key: str, value: str) -> None:
        ...


fs: Cache = CacheFilesystem()
fs.set('name', 'Jan Twardowski')
fs.is_valid('name')
fs.get('name')

ram: Cache = CacheRAM()
ram.set('name', 'Jan Twardowski')
ram.is_valid('name')
ram.get('name')

db: Cache = CacheDatabase()
db.set('name', 'Jan Twardowski')
db.is_valid('name')
db.get('name')

4.12.4. Assignments

4.12.4.1. OOP Interface Iris

English
  1. Use code from "Input" section (see below)

  2. Define interface IrisInterface

  3. Define methods sum(), avg(), len() in IrisInterface

  4. Methods must raise exception NotImplementedError

  5. Create class Setosa inheriting from IrisInterface

  6. Create instance of a class Setosa and call avg() method

  7. Create instance of a class IrisInterface and call avg() method

Polish
  1. Użyj kodu z sekcji "Input" (patrz poniżej)

  2. Zdefiniuj interfejs IrisInterface

  3. Zdefiniuj metody sum(), avg(), len() w IrisInterface

  4. Metody muszą podnosić wyjątek NotImplementedError

  5. Stwórz klasę Setosa dziedziczące po IrisInterface

  6. Stwórz instancje klasy Setosa i wywołaj metodę avg()

  7. Stwórz instancje klasy IrisInterface i wywołaj metodę avg()

Input
class IrisInterface:
    def __init__(self, sepal_length, sepal_width, petal_length, petal_width):
        raise NotImplementedError
iris = IrisInterface(5.8, 2.7, 5.1, 1.9)
setosa = Setosa(5.1, 3.5, 1.4, 0.2)
Output
setosa = Setosa(5.1, 3.5, 1.4, 0.2)
print(setosa.avg())
# 2.55

iris = IrisInterface(5.8, 2.7, 5.1, 1.9)
# NotImplementedError
Hint
  • self.__dict__