Dynamic class methods misunderstanding

Alex Martelli aleaxit at yahoo.com
Sat Jan 29 04:24:27 EST 2005


Bill Mill <bill.mill at gmail.com> wrote:
   ...
> > 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)

Another approach with the same result is to exploit the fact that a
function is a descriptor:

self.m = method.__get__(self, Test)


Alex



More information about the Python-list mailing list