【发布时间】:2016-07-30 08:12:10
【问题描述】:
class B(object):
def meth(self):
pass
class C(B):
pass
b = B()
c = C()
print c.meth.__get__(b, B)
给我:
<bound method C.meth of <__main__.C object at 0x10a892910>>
但我希望是这样的:
<bound method B.meth of <__main__.B object at 0x?????????>>
为什么我对c.meth.__get__ 的手动调用被忽略了? Python 的行为就像我写了print c.meth,并且该查找自动转换为print c.meth.__get__(c, C)
【问题讨论】:
-
c.meth是一个绑定的方法对象。坦率地说,我很惊讶它有自己的__get__方法。没有理由期望它的工作方式与原始函数对象的描述符相同。
标签: python python-2.7 inheritance methods descriptor