简体   繁体   中英

Abstract method inheritance in Python

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...

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...)

eg:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM