基于Theran的代码,但将其扩展为类的方法:
class Dynamo(object): passdef add_dynamo(cls,i): def innerdynamo(self): print "in dynamo %d" % i innerdynamo.__doc__ = "docstring for dynamo%d" % i innerdynamo.__name__ = "dynamo%d" % i setattr(cls,innerdynamo.__name__,innerdynamo)for i in range(2): add_dynamo(Dynamo, i)d=Dynamo()d.dynamo0()d.dynamo1()
应该打印:
in dynamo 0in dynamo 1
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)