Chaning instance methods

Christian Tismer tismer at appliedbiometrics.com
Wed Apr 7 04:51:35 EDT 1999


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




More information about the Python-list mailing list