This list shows what methods you need to implement for your class to be "regarded" as Sequence: __getitem__
, __len__
, __contains__
, __iter__
, __reversed__
, index
, and count
. So why does this minimal implementation does not work, i.e. why 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 :-(
Is there an additional method I need to implement that I overlooked? Did I misunderstand abstract base classes? Subclassing Sequence
makes issubclass
return True
of course, but that kinda defeats the idea behind abc, doesn't it?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…