4
有没有可能以任何方式将函数添加到类的现有实例? (最有可能只在当前交互式会话,当有人要添加一个方法,而不重新实例有用)在Python中创建一个类后添加一个方法
Example类:
class A():
pass
实例方法添加(引用自这里重要):
def newMethod(self):
self.value = 1
输出:
>>> a = A()
>>> a.newMethod = newMethod # this does not work unfortunately, not enough args
TypeError: newMethod() takes exactly 1 argument (0 given)
>>> a.value # so this is not existing
尽快回复,谢谢。 – PascalVKooten