I've been reading the article on the data model of Python on its Reference website, and have been confused about this part:
When the attribute is a user-defined method object, a new method object is only created if the class from which it is being retrieved is the same as, or a derived class of, the class stored in the original method object; otherwise, the original method object is used as it is.
Essentially, I cannot really conjure up two scenarios where in one the method object is is the same as the original method object, and in the other a new method object is created. Here's the closest I can come:
class TestClass: def TestFunc(): pass class TestClass2: x_attr = TestClass().TestFunc y_attr = TestClass.TestFunc def __init__(self): print "x's class is " + repr(TestClass2.x_attr.__class__) print "y's class is " + repr(TestClass2.y_attr.__class__) if __name__ == '__main__': tc = TestClass() tc2 = TestClass2() print "tc.TestFunc: ".ljust(20) + str(id(tc.TestFunc)) # retrieved from TestClass print "TestClass.TestFunc: ".ljust(20) + str(id(TestClass.TestFunc))# retrieved from TestClass print "tc2.x_attr: ".ljust(20) + str(id(tc2.x_attr)) # retrieved from TestClass2 print "tc2.y_attr: ".ljust(20) + str(id(tc2.y_attr)) # retrieved from TestClass2
However, the output of the different test cases is not as I would expect by reading the passage from the Reference:
x's class is <type 'instancemethod'> y's class is <type 'instancemethod'> tc.TestFunc: 140591510137584 TestClass.TestFunc: 140591510137584 tc2.x_attr: 140591509970288 tc2.y_attr: 140591510137424
Specifically, I was expecting
TestClass2.y_attr) to be the same based on what is cited from the Reference.
Furthermore, I was expecting to see the same outcome for the <
TestClass.TestFunc> comparison pair and the <
tc2.y_attr> comparison pair (i.e. each pair the same or neither the same).
Could you please clarify why this is and what exactly the Reference is trying to say?
P.S. Using Python 2.7.6 on Ubuntu 14.04.