I want to create an abstract class such that subclasses will raise an error when instanced if they don't implement an abstract property. An error should be also raised if the attribute is a method rather than a property. My attempt:
from abc import ABC, abstractmethod
class IClass(ABC):
@property
@abstractmethod
def attr(self):
pass
Unfortunately, this does nothing to prevent instantiating subclasses with an attr
method rather than a property.
The solution should produce the following results:
class C(IClass):
pass
C() # Must fail because 'attr' has not been implemented
class C(IClass):
def attr(self):
pass
C().attr # Must fail because attribute 'attr' is a method rather than a property
class C(IClass):
attr = 'attr'
C().attr # Must pass because 'attr' is a property
The manual: docs.