Python标准库--abc模块

abc--抽象基类

注册一个具体类

class PluginBase(metaclass=abc.ABCMeta):
    @abc.abstractmethod
    def load(self, input):
        pass

    @abc.abstractmethod
    def save(self, output, data):
        pass

class LocalBaseClass(object):
    pass

class RegisteredImplementation(LocalBaseClass):
    def load(self, input):
        return input.read()

    # def save(self, output, data):
    #     return output.write(data)
# 注册,可以不必实现所有抽象方法
PluginBase.register(RegisteredImplementation)

print(issubclass(RegisteredImplementation, PluginBase))
print(isinstance(RegisteredImplementation(), PluginBase))

继承实现

class SubclassImpementation(PluginBase):

    def load(self, input):
        return input.read()

    def save(self, output, data):
        return output.write(data)

# 继承,必须实现所有抽象方法
print(issubclass(SubclassImpementation, PluginBase))
print(isinstance(SubclassImpementation(), PluginBase))

读写属性

class Implementation():
    _value = 'Default value'

    def value_getter(self):
        return self._value

    def value_setter(self, newvalue):
        self._value = newvalue

    value = property(value_getter, value_setter)
    
    # @property
    # def value(self):
    #     return self._value

转载于:https://www.cnblogs.com/wj5633/p/7045560.html

你可能感兴趣的:(Python标准库--abc模块)