difference between class methods and instance methods

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


Diez B. Roggisch wrote:

>> This is badly wrong. John was correct.
>> 
>> Bound methods get created whenever you reference a method of an
>> instance. If you are calling the method then the bound method is
>> destroyed as soon as the call returns. You can have as many different
>> bound methods created from the same unbound method and the same
>> instance as you want: 
> 
> That did escape me so far - interesting. Why is it that way? I'd
> expect that creating a bound method from the class and then storing it
> in the objects dictionary is what happens. 
> 
If you had a class with 50,000 methods it could take quite a while to 
create instances, consider Zope as an example where every object acquires 
large numbers of methods most of which are completely irrelevant. Even 
outside Zope most objects never have all their methods called: dict, for 
example, has 38 methods but most of them get little or no use.

Creating a bound method is a comparatively cheap operation, its a small 
object which just has to hold references to the instance and the function.
The memory allocation is fast, because Python keeps lists of appropriately 
sized buckets for small objects.



More information about the Python-list mailing list