October 2018
Beginner to intermediate
466 pages
12h 2m
English
As we saw earlier, it's not necessary to have an abstract base class to enable duck typing. However, imagine we were creating a media player with third-party plugins. It is advisable to create an abstract base class in this case to document what API the third-party plugins should provide (documentation is one of the stronger use cases for ABCs). The abc module provides the tools you need to do this, but I'll warn you in advance, this utilizes some of Python's most arcane concepts, as demonstrated in the following block of code::
import abc class MediaLoader(metaclass=abc.ABCMeta): @abc.abstractmethod def play(self): pass @abc.abstractproperty def ext(self): pass @classmethod def __subclasshook__(cls, C): if ...