名前のリストがあるとします。これらをインスタンス関数としてクラス インスタンスに動的に追加できるようにしたいと考えています。私は types.MethodType について知っていますが、そこからここにたどり着くのは少し初心者です。基本的に私がやりたいことは次のとおりです。
class foo( object ):
def __init__(self):
pass
f = foo()
names = ["a","b","c"]
for name in names:
add name() to f # not sure what to do here
# what I wanted added to instance "f" is this for each name:
def name(self, *args, **kwargs):
print( "My name is %s" % inspect.stack()[0][3] )
print( "__called, args=%r, **kwargs=%r" % (args, kwargs) )
f.a() # ==> calls f.a()
f.b(1,2,3) # calls f.b(1,2,3 )and so on