Demystifying the Magic
Learn about the abstract base classes, metaclasses, and Python's type class for class construction, extension, and binding operators.
We'll cover the following...
We’ve covered various concepts in the previous lesson to grasp how we can create our own base classes. It’s clear they’re doing a lot of work for us. Let’s look inside the class to see some of what’s going on:
from Dice import Dieprint(Die.__abstractmethods__)print(Die.roll.__isabstractmethod__)
In the Dice.py
file, the abstract method, roll()
, is tracked in a specially named attribute, __abstractmethods__
, of the class. This suggests what the @abc.abstractmethod
decorator does. Then in the main.py
file, this decorator sets __isabstractmethod__
to mark the method. When Python finally builds the class from the various methods and attributes, the list of abstractions is also collected to create a class-level set of methods that must be implemented.
Any subclass that extends ...