Here I have a class hierarchy, where I want to enforce all subclasses of AnimalWithFur
to define a property of fur_type
:
from abc import ABC, abstractmethod
class Animal:
...
class AnimalWithFur(ABC, Animal):
@property
@abstractmethod
def fur_type(self) -> str:
...
class Dog(AnimalWithFur):
...
dog = Dog()
print(dog.fur_type)
This works fine. When I try to instantiate dog
, it will raise the expected exception.
But, let's say I want to spice things up and make AnimalWithFur a dict
instead of an Animal
:
from abc import ABC, abstractmethod
class Animal:
...
class AnimalWithFur(ABC, dict):
@property
@abstractmethod
def fur_type(self) -> str:
...
class Dog(AnimalWithFur):
...
dog = Dog()
print(dog.fur_type)
This code no longer works (it does not throw an exception that Dog() hasn't defined fur_type anymore...)
Why doesn't it work, and what can I do to fix it?