开发者

Why aren't bound instance methods in python reference equal?

开发者_C百科
>>> 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


They're bound at runtime; accessing the attribute on the object rebinds the method anew each time. The reason they're different when you put both on the same line is that the first method hasn't been released by the time the second is bound.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜