class mc:
def show(self):
self.func()
a = mc()
def myfunc(self):
print('instance function')
a.func = myfunc
a.show()
以上不起作用:TypeError: myfunc() missing 1 required positional argument: 'self'.
考慮到我使用的是點表示法,為什么 Python 不會自動插入實體名稱?
uj5u.com熱心網友回復:
您可以使用猴子補丁動態地將方法添加到類中。
class mc:
def show(self):
self.func()
a = mc()
def myfunc(self):
print('instance function')
mc.func = myfunc
a.show()
uj5u.com熱心網友回復:
它可以像這樣作業,因為該函式不是真正的實體方法
class mc:
def show(self):
self.x = 1
func(self)
a = mc()
def myfunc(self):
print (self.x)
print('instance function')
a.func = myfunc
a.show()
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/424870.html
上一篇:我如何用串列呼叫特定函式
