Python does not have the traditional interface keyword as seen in other programming languages. Instead, the language supports ducktyping and multiple inheritance which may satisfy the need for interfaces for some use cases.
However, there are native pythonic methods to create / implement an interface.
Ducktyping
If it quacks like a duck and walks like a duck, Python assumes it’s a duck:
fromdataclassesimportdataclass@dataclassclassUser:id:strname:strclassUserService:defget_user_by_id(self,id:str)->User:"""Retrieve user by ID"""passclassDatabase(UserService):defget_user_by_id(self,id:str)->User:"""Retrieves user by ID from an database"""# Implementation goes herepassclassCache(UserService):defget_user_by_id(self,id:str)->User:"""Retrieves user by ID from cache"""# Implementation goes herepass
Due to the lack of enforcement, this paradigm may not scale well for the implementer’s needs.
Abstract Base Classes
Introduced in Python 2.6, abstract base classes allow for interface creation.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
fromabcimportABC,abstractmethodfromdataclassesimportdataclass@dataclassclassUser:id:strname:strclassUserService(ABC):@abstractmethoddefget_user_by_id(self,id:str)->User:"""Retrieve user by ID"""raiseNotImplementedErrorclassDatabase(UserService):defget_user_by_id(self,id:str)->User:"""Retrieves user by ID from a database"""# Implementation goes herepass
This covers two cases:
The inheriting class must implement the abstract method
The implementation cannot instantiate this parent’s method: super().get_user_by_id(id)