Dynamic class methods misunderstanding

Bill Mill bill.mill at gmail.com
Fri Jan 28 11:17:59 EST 2005


Diez,

On Fri, 28 Jan 2005 16:57:37 +0100, Diez B. Roggisch <deetsNOSPAM at web.de> wrote:
> >
> > Why doesn't m get the implicit self parameter in the self.method()
> > call? How would I make it a proper member of the class, so that a
> > self.method() call would work with the above "m" function?
> 
> Use new.instancemethod:
> 
> import new
> 
> class Test:
>     def __init__(self, method):
>         self.m = new.instancemethod(method, self, Test)
> 

Beautiful! thank you very much. Looking into the "new" module in
python 2.4, that's equivalent to:

self.m = type(self.__init__)(method, self, Test)

I didn't know that you could call types to create another type.

Peace
Bill Mill
bill.mill at gmail.com



More information about the Python-list mailing list