June 2019
Beginner to intermediate
770 pages
19h 24m
English
We can define abstract base classes with complex rules for overrides to create concrete subclasses. This is done by implementing the __subclasshook__() method of the abstract base class, as shown in the following code:
class AbstractBettingStrategy2(ABC): @abstractmethod def bet(self, hand: Hand) -> int: return 1 @abstractmethod def record_win(self, hand: Hand) -> None: pass @abstractmethod def record_loss(self, hand: Hand) -> None: pass @classmethod def __subclasshook__(cls, subclass: type) -> bool: """Validate the class definition is complete.""" if cls is AbstractBettingStrategy2: has_bet = any(hasattr(B, "bet") for B in subclass.__mro__) has_record_win = any(hasattr(B, "record_win") for B in subclass. ...
Read now
Unlock full access