Mailing List Archive

Methods and friends
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.)

or

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

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/
Re: Methods and friends [ In reply to ]
> 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

Are you sure? As far as I know, steps 3 and 4 are combined when you
do getattr on an instance: instance_getattr() calls
instance_getattr1() which calls class_lookup(). The latter doesn't
create an unbound method. instance_getattr1() then turns it into a
bound method.

--Guido van Rossum (home page: http://www.python.org/~guido/)
Re: Methods and friends [ In reply to ]
Guido van Rossum wrote:
>
> > 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
>
> Are you sure? As far as I know, steps 3 and 4 are combined when you
> do getattr on an instance: instance_getattr() calls
> instance_getattr1() which calls class_lookup(). The latter doesn't
> create an unbound method. instance_getattr1() then turns it into a
> bound method.

Oops. Correct. I should have looked a little closer. So it
already works in the first way I mentioned as improvement... your
time machine again ;-)

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