Abstract method inheritance in Python
abc, abstract, inheritance, python
Solution
You've pretty much got all the code there, you can always test it and see if it works ... but as a spoiler, Your design is fine so long as `C.test_attribute` gets decorated with `property`.
If you try to make an instance of `B`, then you'll have problems since the whole abstract interface hasn't been created, but it is fine to create it as a base class for `C` (and presumably other classes later...)
e.g.:
import abc
class A(object):
__metaclass__ = abc.ABCMeta
@abc.abstractproperty
def foo(self):
pass
class B(A):
def bar(self):
return "bar"
class C(B):
@property
def foo(self):
return "foo"
print C().foo # foo
print C().bar() # bar
print B().foo # TypeError
Problem
Let's assume that we have a Python class that makes use of the abc module to define an abstract attribute: ``` import abc class A(object): __metaclass__ = abc.ABCMeta @abc.abstractproperty def test_attribute(self): raise NotImplementedError ``` Let's now consider to define `B` that subclasses from `A` by adding a new method (`test_method()`), and `C` that subclasses from `B` implementing the abstract method originally declared in `A`: ``` class B(A): def test_method(self): pass class C(B): def test_attribute(self): # Implement abstract attribute pass ``` Assuming that I would like to keep `B` abstract (non-instantiable), shall I redefine the abstract property (`test_attribute`) and the metaclass assignment also in `B`? Or is it enough to inherit them from `A` (as in the above code)? I know that Python allows me to not redefine the abstract methods and thus inherit them from the parent class. Is this correct from a theoretical software engineering perspective? I'm asking so because if I'm not wrong other languages (such as Java) do not allow inheritance of abstract methods without reimplementing them as abstract...