From: tismer at appliedbiometrics.com (Christian Tismer)
Date: Wed, 7 Apr 1999 08:51:35 GMT
Subject: Chaning instance methods
References: <000401be80bc$999f1820$699e2299@tim>
Message-ID: <370B1C97.AC3CFD8A@appliedbiometrics.com>
Content-Length: 1394
X-UID: 280                                                  


Tim Peters wrote:

<snip/>

> Right, a direct attribute of an instance is never a method.  Except that
> this "works":
> 
> import new
> f.m = new.instancemethod(m2, f, Foo)
> f.m()
> 
> This sets f.m to a *bound* instance method that refers to f, which Python
> treats as an ordinary function when later referenced via f.m.  Without using
> the "new" module, you can get the same effect via e.g.
> 
> old_Foo_m = Foo.m
> Foo.m = m2
> f.m = f.m  # heh heh <wink>
> Foo.m = old_Foo_m

As a related topic, there is also DOnBeaudry's functor module from
March '97 which makes use of the newmodule internally and is able
to create quite efficient wrappers around callables with bound
parameters. The calling overhead is about twice as large
as for the original function.

This version needed some tweaking to make it run under Python 1.5 .
I just uploaded a slightly modified version to incoming and
hope it will make it into contrib/system.

cheers - chris

-- 
Christian Tismer             :^)   <mailto:tismer at appliedbiometrics.com>
Applied Biometrics GmbH      :     Have a break! Take a ride on Python's
Kaiserin-Augusta-Allee 101   :    *Starship* http://starship.python.net
10553 Berlin                 :     PGP key -> http://wwwkeys.pgp.net
PGP Fingerprint       E182 71C7 1A9D 66E9 9D15  D3CC D4D7 93E2 1FAE F6DF
     we're tired of banana software - shipped green, ripens at home




