difference between class methods and instance methods

Duncan Booth duncan.booth at invalid.invalid
Fri Feb 18 04:27:04 EST 2005


John wrote:

>>inst = C()
>>f1 = inst.foo
>>f2 = inst.foo
>>f1, f2
>> 
>> (<bound method C.foo of <__main__.C instance at 0x00B03F58>>, <bound
>> method C.foo of <__main__.C instance at 0x00B03F58>>)
>> 
> 
> I just wanted to interject, although those two hex
> numbers in the above line are the same, calling
> id() on f1 and f2 produces two *different* numbers,
> which agrees with the point you made.

Yes, note that the numbers are in the repr of C() and are actually 
id(inst).

A common mistake seems to be for people to do:

>>> inst = C()
>>> id(inst.foo), id(inst.foo)
(11803664, 11803664)

and conclude that the bound methods are the same. They aren't, but if you 
just take the id of an object and throw it away then the next object of 
similar size can [and with the current implementation probably will] be 
allocated at the same location.



More information about the Python-list mailing list