[英]Why is this python class instance hashable?
这与python 2.x有关
在下面的类中,如果我们继承“ object ”,我理解这些方法是在包含__hash__
的派生类Foo中继承的(可以通过__hash__
dir(Foo())看到这个
因此调用hash(Foo())调用魔术方法__hash__
并给我们一个哈希值。
但是,如果我们没有子类“ object ”,导致dir(Foo())没有列出__hash__
方法,那么为什么我们仍然在python2中得到一个哈希值?
我相信python3这个问题已经解决了以来的方法 默认情况下, “object *”类是继承的。
#class Foo(object) Works since __hash__ is available in the base class
class Foo: #Why does this work?
def __init__(self):
self.x = None
a = Foo()
print dir(a) # No __hash__ magic method
print hash(a)
# Expecting an error like non-hashable or __hash__ not implemented
# or something similar
旧式的课很奇怪。 正式地说,旧式类的实例并不是它们类的完全实例,它们都是类型instance
。 instance
类型定义了__hash__
( tp_hash
是C级槽,相当于C定义类型的__hash__
),所以即使它没有直接在你的实例上定义,也没有在创建它的类上定义,它通过instance
类型自身找到__hash__
奇怪而可怕的魔法(实际上,神奇的是它如何设法使用你的类的功能,因为它的类型是instance
)。
您可以在交互式解释器中看到:
>>> class Foo: pass
>>> Foo().__hash__ # Same basic error for Foo.__hash__ too
AttributeError Traceback (most recent call last)
...
----> 1 Foo().__hash__
AttributeError: Foo instance has no attribute '__hash__'
>>> type(Foo())
<type 'instance'>
>>> type(Foo()).__hash__
<slot wrapper '__hash__' of 'instance' objects>
即使实例本身看不到__hash__
这也__hash__
因为“特殊方法”(那些以双下划线开头和结尾的特殊方法)会在类型而不是实例上查找,因此在instance
本身上可以找到__hash__
。 在C级别, hash(x)
正在执行type(x).__hash__(x)
(它有点复杂,因为如果__eq__
有自定义定义它不会使用默认的__hash__
实现,但这是一般的理念)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.