Python Interfaces
2021-03-16
Since Python uses duck typing, are not necessary. The original attitude, which held sway for many years, is that you don't need them: Python works on the EAFP (easier to ask forgiveness than permission) principle.
This works pretty well, but there are definite use cases for interfaces, especially with larger software projects. The final decision in Python was to provide the abc module, which allows you to write abstract base classes
Example
import abc
class NotImplementedError(Exception):
pass
class Piece(metaclass=abc.ABCMeta):
@abc.abstractmethod
def move(self, **kargs):
raise NotImplementedError
class Queen(Piece):
def move(self, square):
# Specific implementation for the Queen's movements
pass
class King(Piece):
pass
# Allows you to create an instance of the Queen class
queen = Queen()
# Does not allow you to create an instance of the Queen class,
# because the abstract method move() is not implemented.
king = King()
References
Implementing an Interface in Python