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