【发布时间】:2016-11-29 14:05:29
【问题描述】:
根据 Python 2.7.12 文档,
当属性是用户定义的方法对象时,新方法 仅当从中检索对象的类时才创建对象 与存储在 原始方法对象; 否则,原来的方法对象是 照原样使用。
我试图通过编写代码来理解这一段:
# Parent: The class stored in the original method object
class Parent(object):
# func: The underlying function of original method object
def func(self):
pass
func2 = func
# Child: A derived class of Parent
class Child(Parent):
# Parent.func: The original method object
func = Parent.func
# AnotherClass: Another different class, neither subclasses nor subclassed
class AnotherClass(object):
# Parent.func: The original method object
func = Parent.func
a = Parent.func
b = Parent.func2
c = Child.func
d = AnotherClass.func
print b is a
print c is a
print d is a
输出是:
False
False
False
我希望它是:
False
False
True
【问题讨论】:
-
试试:
print Parent.func is Parent.func. -
@Robᵩ
print Parent.func is Parent.func给出False,但print a is a给出True。
标签: python python-2.7 object inheritance methods