Adding bound methods dynamically...

Gregory Bond gnb at
Wed Aug 31 10:04:39 CEST 2005

Kevin Little wrote:

> I want to dynamically add or replace bound methods in a class.  I want

I asked a seemingly-unrelated question a week or so ago, and learned 
something interesting:

Python 2.3.4 (#2, Jul 12 2004, 12:46:36)
[GCC 3.3] on sunos5
Type "help", "copyright", "credits" or "license" for more information.
 >>> def foo(self):
..     print "foo called"
 >>> class C(object):
..     pass
 >>> type(foo)
<type 'function'>
 >>> = foo
 >>> type(
<type 'instancemethod'>
 >>> c = C()
foo called
 >>> type(
<type 'instancemethod'>

I.e. assigning a normal function object to a class object turns it into 
a member function!

You can read more in the thread with the subject 'keeping a ref to a 
non-member function in a class'.

More information about the Python-list mailing list