Skip to content Skip to sidebar Skip to footer

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:

classPet(ABC):
    @abstractmethod    @propertydefname(self)
        pass    @abstractmethoddefmake_noise(self):
        raise NotImplementedError("Needs to implement in subclass")

    defprint_my_name(self):
        print(self.name)

With this you can add a setter as well with @name.setter.

Solution 2:

Post a Comment for "Force Implementing Specific Attributes In Subclass Python"