Interfacce

Programmazione a oggetti intermedia in Python

Jake Roach

Data Engineer

Interfacce

Un tipo speciale di classe composta solo da metodi astratti che crea un contratto con le classi che implementano l'interfaccia

  • Devi definire metodi astratti
  • Struttura (scheletro) delle definizioni dei metodi
  • I parametri non devono coincidere
  • Formale o informale

$$

$$

Iniziamo dalle interfacce informali!

Diagramma di un'interfaccia Python con tre metodi astratti.

Programmazione a oggetti intermedia in Python

Interfacce informali con 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
  • Metodi astratti assign_homework() e grade_assignment()
Programmazione a oggetti intermedia in Python

Interfacce informali con 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...
Programmazione a oggetti intermedia in Python

Interfacce formali con 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

In cosa le interfacce formali differiscono dalle classi base astratte?

Programmazione a oggetti intermedia in Python

Interfacce vs. classi base astratte

Interfacce

  • Definiscono un "contratto" con le classi che le implementano
  • "Cose da fare"
  • Formali e informali
  • Contengono solo metodi astratti (nessun metodo concreto)
  • Classi quasi identiche

Classi base astratte

  • Creano un "progetto" per classi con caratteristiche comuni
  • "È-un"
  • Tipicamente solo formali
  • Mix di metodi astratti e concreti
  • Classi che appaiono e si comportano in modo simile
Programmazione a oggetti intermedia in Python

Implementare un'interfaccia formale

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")
Programmazione a oggetti intermedia in Python

Errore nell'implementare un'interfaccia formale

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
Programmazione a oggetti intermedia in Python

Esercitiamoci!

Programmazione a oggetti intermedia in Python

Preparing Video For Download...