このリストは、クラスが Sequence として「見なされる」ために実装する必要があるメソッドを示しています: __getitem__、__len__、__contains__、__iter__、__reversed__、indexおよびcount. では、なぜこの最小限の実装が機能しないのissubclass(S, Sequence) is Falseでしょうか。
from collections import *
class S(object):
def __getitem__(self, item):
raise IndexError
def __len__(self):
return 0
def __contains__(self, item):
return False
def __iter__(self):
return iter(())
def __reversed__(self):
return self
def index(self, item):
raise IndexError
def count(self, item):
return 0
issubclass(S, Iterable) # True :-)
issubclass(S, Sized) # True :-)
issubclass(S, Container) # True :-)
issubclass(S, Sequence) # False :-(
私が見落としていた、実装する必要がある追加の方法はありますか? 抽象基底クラスを誤解していませんか? もちろん、サブクラス化Sequenceは元issubclassに戻りますTrueが、それは abc の背後にあるアイデアを打ち負かしますよね?