Python abstract base class (ABC) example

Published: 30 March 2023
on channel: Pybites
1,537
20

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...
Pybites Search project / code: https://github.com/PyBites-Open-Sourc...
Repository pattern (as featured in the Cosmic Python book): https://www.cosmicpython.com/book/cha...
Subclasses article: https://pybit.es/articles/python-subc...


On this page of the site you can watch the video online Python abstract base class (ABC) example with a duration of hours minute second in good quality, which was uploaded by the user Pybites 30 March 2023, share the link with friends and acquaintances, this video has already been watched 1,537 times on youtube and it was liked by 20 viewers. Enjoy your viewing!