adding instance methods after instantiation
Chris Gonnerman
chris.gonnerman at newcenturycomputers.net
Sat Jun 23 10:33:33 EDT 2001
----- Original Message -----
From: "Skip Montanaro" <skip at pobox.com>
> Lee> Is there some way to make m2 an instance method so that it will
> Lee> automatically get passed c1 as its first argument?
>
> Check out the instancemethod function of the new module.
Here is code based on Lee's sample:
######################################################
### adding an instance method after the fact
import new
class c:
def __init__(self):
self.a1 = 1
def m1(self):
self.a2 = 2
def m2(self): #note, outside of class c definition
self.a3 = 3
c1 = c()
c1.m2 = new.instancemethod(m2, c1, c)
c1.m2()
print c1.a3
More information about the Python-list
mailing list