其他
Python抽象基类的定义与使用
定义抽象基类的子类
Card = collections.namedtuple('Card', ['rank', 'suit'])
class FrenchDeck2(collections.MutableSequence):
ranks = [str(n) for n in range(2, 11)] + list('JQKA')
suits = 'spades diamonds clubs hearts'.split()
def __init__(self):
self._cards = [Card(rank, suit) for suit in self.suits
for rank in self.ranks]
def __len__(self):
return len(self._cards)
def __getitem__(self, position):
return self._cards[position]
def __setitem__(self, position, value): # <1>
self._cards[position] = value
def __delitem__(self, position): # <2>
del self._cards[position]
def insert(self, position, value): # <3>
self._cards.insert(position, value)
__setitem__
、__delitem__
、insert
,所以FrenchDeck2类必须实现它们。而对于其他非抽象方法比如append
、extend
、pop
等,则可以直接继承无需实现。注意,Python只会在运行时实例化FrenchDeck2类时真正检查抽象方法的实现,如果未实现会抛出TypeError异常,提示Can't instantiate abstract class之类的。
标准库中的抽象基类
collections.abc
Itearble通过
__iter__
方法支持迭代Container通过
__contains__
方法支持in运算符Sized通过
__len__
方法支持len()函数
.items()
、.keys()
、.values()
返回的对象分别是ItemsView、KeysView和ValuesView的实例。numbers
Number
Complex
Real
Rational
Integral
isinstance(x, numbers.Integral)
检查一个数是不是整数,这样代码就能接受int、bool(int的子类),再比如使用isinstance(x, numbers.Real)
检查浮点数,这样代码就能接受bool、int、float、fractions.Fraction。定义抽象基类
import abc
class Tombola(abc.ABC): # <1>
@abc.abstractmethod
def load(self, iterable): # <2>
"""Add items from an iterable."""
@abc.abstractmethod
def pick(self): # <3>
"""Remove item at random, returning it.
This method should raise `LookupError` when the instance is empty.
"""
def loaded(self): # <4>
"""Return `True` if there's at least 1 item, `False` otherwise."""
return bool(self.inspect()) # <5>
def inspect(self):
"""Return a sorted tuple with the items currently inside."""
items = []
while True: # <6>
try:
items.append(self.pick())
except LookupError:
break
self.load(items) # <7>
return tuple(sorted(items))
# END TOMBOLA_ABC
继承abc.ABC
使用
@abc.abstractmethod
装饰器标记抽象方法抽象基类也可以包含普通方法
抽象基类的子类必须覆盖抽象方法(普通方法可以不覆盖),可以使用super()函数调用抽象方法,为它添加功能,而不是从头开始实现
再看白鹅类型
Sequence.register(str)
Sequence.register(range)
Sequence.register(memoryview)
class TomboList(list):
...
参考资料: 《流畅的Python》第11章 接口:从协议到抽象基类