为什么 collections.Callable 提供 __contains__()、__hash__()、__len__() 和 __call__()
Why collections.Callable provides __contains__(), __hash__(), __len__() and __call__()
为什么 documentation 说:
class collections.Callable
ABCs for classes that provide respectively the methods contains(), hash(), len(), and call().
而且不只是 __call__()
?
__contains__()
、__hash__()
、__len__()
对于可调用对象的作用是什么?
例如,我需要让我的 类 可哈希吗?
你误解了那段文字的意思。文档涵盖四个 ABCs:
class collections.Container
class collections.Hashable
class collections.Sized
class collections.Callable
句子ABCs for 类中的grammar construct respectively分别提供了方法为四个类中的每一个附加了一个单独的方法在这里,按顺序。
所以Container
提供__contains__()
,Hashable
涵盖__hash__()
,Sized
涵盖__len__()
和Callable
涵盖__call__()
.
也就是说Callable
只提供了一个__call__()
方法,其他3个方法都不提供
为什么 documentation 说:
class collections.Callable
ABCs for classes that provide respectively the methods contains(), hash(), len(), and call().
而且不只是 __call__()
?
__contains__()
、__hash__()
、__len__()
对于可调用对象的作用是什么?
例如,我需要让我的 类 可哈希吗?
你误解了那段文字的意思。文档涵盖四个 ABCs:
class collections.Container
class collections.Hashable
class collections.Sized
class collections.Callable
句子ABCs for 类中的grammar construct respectively分别提供了方法为四个类中的每一个附加了一个单独的方法在这里,按顺序。
所以Container
提供__contains__()
,Hashable
涵盖__hash__()
,Sized
涵盖__len__()
和Callable
涵盖__call__()
.
也就是说Callable
只提供了一个__call__()
方法,其他3个方法都不提供