abc:Abstract Base Classes
作用:在代码中定义和使用抽象基类进行API检查。
1. 为什么使用abc?
Abstract base classes由一组接口组成,检查比
hasattr()
更严格。通过定义一个抽象基类,可以为一组子类定义一个通用的API。这对于第三方为应用提供插件等非常有用,另外当您在一个大型的团队中工作或在一个大型的代码库中,同时将所有的类放在您的头脑中是困难或不可能的时,它也可以帮助您。
2. abc怎么工作
abc通过把基类中的方法标记为抽象方法,并且注册具体类为基类的实现的方式工作。
定义基类: abc_base.py
import abcclass PluginBase(object):__metaclass__ = abc.ABCMeta@abc.abstractmethoddef load(self, input):"""Retrieve data from the input source and return an object."""return@abc.abstractmethoddef save(self, output, data):"""Save the data object to the output."""return
有两种方法表明一个具体类实现了一个抽象类
a) 第一种方法:通过使用abc注册,这种方法下RegisteredImplementation
并不是由PluginBase
派生,而是通过注册方式.
abc_register.py
import abc
from abc_base import PluginBaseclass RegisteredImplementation(object):def load(self, input):return input.read()def save(self, output, data):return output.write(data)PluginBase.register(RegisteredImplementation)if __name__ == '__main__':print 'Subclass:', issubclass(RegisteredImplementation, PluginBase)print 'Instance:', isinstance(RegisteredImplementation(), PluginBase)
output:
Subclass: True
Instance: True
b) 第一种方法:通过实现PluginBase
API,是派生.
abc_subclass.py
import abc
from abc_base import PluginBaseclass SubclassImplementation(PluginBase):def load(self, input):return input.read()def save(self, output, data):return output.write(data)if __name__ == '__main__':print 'Subclass:', issubclass(SubclassImplementation, PluginBase)print 'Instance:', isinstance(SubclassImplementation(), PluginBase)
output:
Subclass: True
Instance: True
两种方式的不同:
SubclassImplementation
在PluginBase.__subclasses__()
中,而RegisteredImplementation
不在.SubclassImplementation
必须实现PluginBase
中的所有抽象方法,否则会在运行时报错;而RegisteredImplement
不需要.
3. 抽象方法的实现
在抽象类中抽象方法也可以提供通用的逻辑实现,这样具体类中就可以通过调用super()
重用抽象方法的实现.
import abc
from cStringIO import StringIOclass ABCWithConcreteImplementation(object):__metaclass__ = abc.ABCMeta@abc.abstractmethoddef retrieve_values(self, input):print 'base class reading data'return input.read()class ConcreteOverride(ABCWithConcreteImplementation):def retrieve_values(self, input):base_data = super(ConcreteOverride, self).retrieve_values(input)print 'subclass sorting data'response = sorted(base_data.splitlines())return responseinput = StringIO("""line one
line two
line three
""")reader = ConcreteOverride()
print reader.retrieve_values(input)
print
output:
base class reading data
subclass sorting data
['line one', 'line three', 'line two']
4. 抽象特性(Abstract Properties)
如果你的API规范中还包括属性,那么你可以使用@abstractproperty
来定义.
import abcclass Base(object):__metaclass__ = abc.ABCMeta@abc.abstractpropertydef value(self):return 'Should never get here'class Implementation(Base):@propertydef value(self):return 'concrete property'try:b = Base()print 'Base.value:', b.value
except Exception, err:print 'ERROR:', str(err)i = Implementation()
print 'Implementation.value:', i.value
因为Base
只有property value
getter方法的抽象版本,所有它不能被实例化.
output:
ERROR: Can't instantiate abstract class Base with abstract methods value
Implementation.value: concrete property
定义抽象的读写特性
import abcclass Base(object):__metaclass__ = abc.ABCMetadef value_getter(self):return 'Should never see this'def value_setter(self, newvalue):returnvalue = abc.abstractproperty(value_getter, value_setter)class PartialImplementation(Base):@abc.abstractpropertydef value(self):return 'Read-only'class Implementation(Base):_value = 'Default value'def value_getter(self):return self._valuedef value_setter(self, newvalue):self._value = newvalue#定义具体类的property时必须与抽象类的abstract property相同。如果只覆盖其中一个将不会工作。value = property(value_getter, value_setter)try:b = Base()print 'Base.value:', b.value
except Exception, err:print 'ERROR:', str(err)try:p = PartialImplementation()print 'PartialImplementation.value:', p.value
except Exception, err:print 'ERROR:', str(err)i = Implementation()
print 'Implementation.value:', i.valuei.value = 'New value'
print 'Changed value:', i.value
output:
ERROR: Can't instantiate abstract class Base with abstract methods value
ERROR: Can't instantiate abstract class PartialImplementation with abstract methods value
Implementation.value: Default value
Changed value: New value
使用装饰器来实现读写的抽象特性,读和写的方法名应该相同
import abcclass Base(object):__metaclass__ = abc.ABCMeta@abc.abstractpropertydef value(self):return 'Should never see this'@value.setterdef value(self, newvalue):returnclass Implementation(Base):_value = 'Default value'@propertydef value(self):return self._value@value.setterdef value(self, newvalue):self._value = newvaluei = Implementation()
print 'Implementation.value:', i.valuei.value = 'New value'
print 'Changed value:', i.value
output:
Implementation.value: Default value
Changed value: New value