Back to Browse

Python abstract base class (ABC) example

1.6K views
Mar 30, 2023
5:11

In today's video we'll show you how you can use ABCs + the abstractmethod decorator to "enforce an interface". First you define a parent class that inherits from the abc.ABC (or uses metaclass=ABCMeta) and decorates one or more methods with the abstractmethod decorator. Subclasses that inherit from this parent class now NEED to implement those abstract methods. Not doing so these sub- or child classes cannot even be instantiated. If this sounds high level, don't worry. We illustrate all this with code from our recent Pybites Search project. We also quickly look at how the "Repository Pattern" typically leverages this technique. Links: - Python abcs (abstract base classes): https://docs.python.org/3/library/abc.html - Pybites Search project / code: https://github.com/PyBites-Open-Source/search - Repository pattern (as featured in the Cosmic Python book): https://www.cosmicpython.com/book/chapter_02_repository.html - Subclasses article: https://pybit.es/articles/python-subclasses/

Download

1 formats

Video Formats

360pmp49.2 MB

Right-click 'Download' and select 'Save Link As' if the file opens in a new tab.

Python abstract base class (ABC) example | NatokHD