python - 为什么 python 引用中的绑定(bind)实例方法不相等?

标签 python equality sentinel

>>> class foo(object):
...     def test(s):
...         pass
...
>>> a=foo()
>>> a.test is a.test
False
>>> print a.test
<bound method foo.test of <__main__.foo object at 0x1962b90>>
>>> print a.test
<bound method foo.test of <__main__.foo object at 0x1962b90>>
>>> hash(a.test)
28808
>>> hash(a.test)
28808
>>> id(a.test)
27940656
>>> id(a.test)
27940656
>>> b = a.test
>>> b is b
True

最佳答案

它们在运行时绑定(bind);每次访问对象上的属性都会重新绑定(bind)该方法。当您将两者放在同一行时它们不同的原因是第一个方法在第二个方法绑定(bind)时尚未发布。

关于python - 为什么 python 引用中的绑定(bind)实例方法不相等?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5384621/

相关文章:

floating-point - 为什么加小数 float 是错误的?

php - 一般错误 : 1364 Field 'location' doesn't have a default value

java - 这个 contains(T Entry) 方法有什么问题? java 。链表

redis - sentinel - 防止特定的奴隶被提升

python - 替换列表中的索引而不重复索引位置

python - 如何从 Python 中的双峰分布生成 n 个随机值?

python - 如何计算图中定义的 N 个允许移动中路径权重的总和,但目标节点未使用 Python 固定

Python:两次时间之差的符号

c++ - 输入/输出迭代器的不等运算符的要求

python - 如何实现 __eq__ 进行集合包含测试?