Python 표준 라이브러리 -- abc 모듈

1466 단어

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

좋은 웹페이지 즐겨찾기