[Python-Dev] Methods and friends

M.-A. Lemburg mal@lemburg.com
Fri, 17 Sep 1999 16:45:52 +0200

I was just looking into classobject.c again after probably more
than a year and wondered... has anyone tried optimizing the method
lookup and binding process recently ?

It seems that for every method call the following happens:
1. the method is looked up in the instance dict; this fails
2. the method is looked up in the class dict; this succeeds
   and returns a function
3. the class then turns the function into a new unbound method
4. the instance sees the unbound method and creates a new
   bound method (deleting the unbound method) and returns it

Two possible improvements:

 let the instance use a special class_getattr_ex function 
  that does not do the extra unbound method step (3.)


 make methods mutable and have step 4. insert the instance
  object into the method object to turn it into a bound

Has anyone tried this ? Does it produce anything noticeable
w/r to method call speed ?

BTW, why does the new module check for instance objects
for the INSTANCE argument ? Methods can handle any object
in this slot, just as they handle any callable object
as "function". Could be put to some use when wrapping
types into classes or vice versa... which is what motivated
the above ;-)

Just curious,
Marc-Andre Lemburg
Y2000:                                                   105 days left
Business:                                      http://www.lemburg.com/
Python Pages:                           http://www.lemburg.com/python/