new
2パターン。
その1。
import new class A(object): pass def test(self): print "test" a = A() a.test = new.instancemethod(test, a, A) print repr(a) # <bound method A.test of <__main__.A object at 0x016D3BD0>> a.test() # test b = A() b.test() # AttributeError
その2。
import types class A(object): pass def test(self): print "test" a = A() a.test = types.MethodType(test, a, A) print repr(a) # <bound method A.test of <__main__.A object at 0x016D3BD0>> a.test() # test b = A() b.test() # AttributeError
まぁ、実質同じなんだけども。以下 new.py より。
from types import MethodType as instancemethod