1

I have a class with the following implementation:

class Device(AbstractDevice):
    @property
    def value(self):
        return 1

it uses this ABC:

from abc import ABC
from abc import abstractmethod

class AbstractDevice(ABC):
    @property
    @abstractmethod
    def value(self):
        pass
    @value.setter
    @abstractmethod
    def value(self):
        pass

The problem is, python doesn't seem to enforce the setter part. Am I missing some decorator or is that by design?

What I would like to happen is if a user implements a class using my AbstractDevice the way I've done here, they'll get an error saying they need to implement the value.setter. Is that at all possible?

  • Does this answer your question? [Force child class to override parent's methods](https://stackoverflow.com/questions/44576167/force-child-class-to-override-parents-methods) – jarmod Aug 09 '21 at 19:37
  • no, because it will only enforce that `value` method/property is implemented, but it does nothing to guarantee that its setter is implemented. the tricky part is that both the property and its setter are named the same in python, so as soon as ABC detects the first 'value' it stops checking – PotatoPeeler Aug 09 '21 at 19:59

0 Answers0