下面是如何使用Python docs中的__subclasshook__
的示例:
@classmethod
def __subclasshook__(cls, C):
if cls is MyIterable:
if any("__iter__" in B.__dict__ for B in C.__mro__):
return True
return NotImplemented
已经有人问了why the "if cls" check is necessary,但还有一件事我不明白。为什么要显式地查看__mro__
中的每个__dict__
?一个人不能说if hasattr(C, '__iter__')
?我用一个玩具的例子尝试了后一种方法,效果和我预期的一样,但是我想知道是否有一些细微的差别,或者文档中的样式是不是因为“历史原因”才这样
目前没有回答
相关问题 更多 >
编程相关推荐