Adding bound methods dynamically...

Gregory Bond gnb at itga.com.au
Wed Aug 31 04:04:39 EDT 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'>
 >>> C.foo = foo
 >>> type(C.foo)
<type 'instancemethod'>
 >>> c = C()
 >>> c.foo()
foo called
 >>> type(c.foo)
<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