如何创建从Python中的抽象类继承的抽象类?

时间:2019-07-26 16:49:16

标签: python abstract-class multiple-inheritance

我想定义一个从另一个抽象类继承的抽象类。

class DataBuffer(ABC):
    def __init__(self):
        self.buffered_data = {}

class IMUDataBuffer(ABC, DataBuffer):
    def __init__(self):
        super(IMUDataBuffer, self).__init__()

    def update(self, message):
        timestamp = message['ts']
        x_val, y_val, z_val = message[self.message_key]
        self.buffered_data[timestamp] = (x_val, y_val, z_val)

class GyroscopeDataBuffer(IMUDataBuffer):
    def __init__(self):
        super(GyroscopeDataBuffer, self).__init__()
        self.message_key = 'gy'

class AccelerometerDataBuffer(IMUDataBuffer):
    def __init__(self, message):
        super(AccelerometerDataBuffer, self).__init__()
        self.message_key = 'ac'

在此示例中,IMUDataBuffer应该继承DataBuffer的方法,但是它永远不会被实例化,因此应该是一个抽象类。

这是我在上面的代码中收到的错误:

TypeError: Cannot create a consistent method resolution order (MRO) for bases ABC, DataBuffer

作为一种廉价的解决方法,我可以在IMUDatabuffer中定义message_key,但这对我来说有点草率,因为IMUDataBuffer实际上不应该具有message_key属性。

class IMUDataBuffer(DataBuffer):
    def __init__(self):
        super(IMUDataBuffer, self).__init__()
        self.message_key = None

在这里处理继承的正确方法是什么?

3 个答案:

答案 0 :(得分:0)

我建议删除IMUDataBuffer父级列表中的ABC,因为DataBuffer已经具有ABC作为父级。

ABC -> DataBuffer -> IMUDataBuffer -> Gyroscope
                                   -> Accelerometer

代码如下:

class ABC:
    pass

class DataBuffer(ABC):  # <-- brings already all stuff from ABC
    def __init__(self):
        self.buffered_data = {}

class IMUDataBuffer(DataBuffer):  # <-- ABC removed here
    def __init__(self):
        super(IMUDataBuffer, self).__init__()

    [...]

然后错误消失。

答案 1 :(得分:0)

在这里已解决-

from abc import *

class DataBuffer(ABC):
    def __init__(self):
        self.buffered_data = {}

class IMUDataBuffer(DataBuffer, ABC):
    def __init__(self):
        super(IMUDataBuffer, self).__init__()

    def update(self, message):
        timestamp = message['ts']
        x_val, y_val, z_val = message[self.message_key]
        self.buffered_data[timestamp] = (x_val, y_val, z_val)

有关详细说明,也请参阅此- Why is this an ambiguous MRO?

答案 2 :(得分:0)

在Python中,仅继承自ABC不会阻止实例化。效果很好:

from abc import ABC

class Foo(ABC):
    pass

foo = Foo()

仅当存在未实施的abstractmethod时,实例化才会引发错误。

您的中间类不必从ABC继承,而仅从DataBuffer继承。只要存在未实现的abstractmethod,实例化就会失败。

from abc import ABC, abstractmethod

class Foo(ABC):
    @abstractmethod
    def do_thing(self):
        pass

class Bar(Foo):
    pass

bar = Bar()  # Instantiation raises TypeError because Bar does not implement do_thing

由于Bar继承自Foo,Foo是抽象类,所以Bar也是抽象类:

>>> type(Foo)
<class 'abc.ABCMeta'>
>>> type(Bar)
<class 'abc.ABCMeta'>

与其他一些随机类进行比较:

>>> class Baz:
>>>    pass
>>> type(Baz)
<class 'type'>