【发布时间】:2014-08-13 14:40:51
【问题描述】:
为什么第一个结果是False,不应该是True吗?
>>> from collections import OrderedDict
>>> OrderedDict.__repr__ is OrderedDict.__repr__
False
>>> dict.__repr__ is dict.__repr__
True
【问题讨论】:
-
Python 3.3 返回 True
-
@JustinEngel:那是因为 Python 3 取消了未绑定的方法,所有方法都是绑定的。在 Python 3 中尝试
OrderedDict().__repr__ is OrderedDict().__repr__,您会看到相同的行为。 -
我很好奇在实际代码中怎么会涉及到这样的事情
-
@NickT:验证猴子补丁方法需要了解方法是如何绑定的。将一个类的方法添加到另一个类还需要您提取函数对象,而不是方法等。
标签: python python-2.7 methods python-internals