adding instance methods after instantiation

Lee Harr missive at frontiernet.net
Sat Jun 23 12:35:29 EDT 2001


On Sat, 23 Jun 2001 09:33:33 -0500, <chris.gonnerman at newcenturycomputers.net>:
> ----- 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
> 
> 
> 
> 

Thank you!

That's exactly what I did after Skip's message.
This is just what I was looking for.

(Python and comp.lang.python continue to deliver!)




More information about the Python-list mailing list