Force Implementing Specific Attributes In Subclass Python
I'm have a parent class, and I would like to 'force' everyone that will inherit from it to implement some specific class attributes. I don't have this problem with methods, since I
Solution 1:
You can try by using ABCMeta metaclass or ABC. After that you can use the @abstractmethod of an @property. eg:
class Pet(ABC):
@abstractmethod
@property
def name(self)
pass
@abstractmethod
def make_noise(self):
raise NotImplementedError("Needs to implement in subclass")
def print_my_name(self):
print(self.name)
With this you can add a setter as well with @name.setter
.
Solution 2:
use can use python's Abstract Base Class
/ ABS
or Method Overriding
whichever is feasible for you as per your requirements.
Post a Comment for "Force Implementing Specific Attributes In Subclass Python"