The method descriptor is a normal class with the __get__ , __set__ and __del__ .
When, for example, __get__ , 2 or 3 arguments are passed:
self , which is the handle class itself,inst , which is the object of the call to which the described method should be bound,cls , which may be None .
To illustrate method_descriptor technology, let me give this example:
class Descriptor(object): def __init__(self, m): self._meth=m def __get__(self, inst, cls=None): if cls is None: cls=type(inst) delattr(cls,self._meth.func_name) def _inst_meth(*a): return self._meth(inst,*a) return _inst_meth def instanceonlymethod(f): return Descriptor(f) class Test(object): def meth_1(self,*a): return '-'.join(str(i) for i in a) @instanceonlymethod def meth_2(self,*a): return '-'.join(str(i) for i in a) t=Test() print t.meth_1(2,3,4,5)
Now when you call Test.meth_2(t, 1,2,3,4) , it will not work.
source share