2014-01-15 79 views
2

我正在動態編程環境中工作,我可能需要定義(或重新定義)類函數。所以考慮這個例如:在類定義之外的Python函數分配會導致參數異常

def func(self): 
    print("hello2 \n") 

class ManClass: 
    def __init__(self): 
     pass 
    def func1(self): 
     print("hello1\n") 

a = ManClass() 

a.func1() 
hello1 

a.func2 = func 
>>> a.func2() 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
TypeError: func() takes exactly 1 argument (0 given) 

如果FUNC2()已定義在類內部 - a.func2()會被解釋爲ManClass.func2(一) - 但現在,我在外面給它分配,它似乎期待一個論點。我如何解決這個問題,但更重要的是,爲什麼這兩個定義之間存在差異?

回答

2

您沒有將func添加到類中,而是將它添加到實例中。改爲嘗試ManClass.func2 = func

a.func2 = func增加func到類的a實例作爲命名func2實例屬性,而不是作爲一個實例成員方法(這是真正爲底層類對象上調用的成員只是特殊處理)。

或者,您也可以使用MethodType將成員方法添加到單個實例,因爲@jonrsharpe在他的答案中指出。

+0

哦,你說得對。類和實例屬性/函數是「不同的」。我應該知道我做錯了什麼。非常感謝 ! – user1922297

2

這是一個函數和一個綁定方法的區別,其中「綁定」是指實例self。要解決你的問題,你需要做的獨立功能MethodType

from types import MethodType 

a.func2 = MethodType(func, a) 

此綁定funcManClass實例a,允許它訪問任何實例屬性。請注意,此僅影響a,其他ManClass實例將保留原始類定義,除非進行類似的修補。

如果只是附加功能

a.func2 = func 

你仍然可以訪問它:

a.func2(None) # will print "hello2 \n" 

但是它沒有得到隱含對象實例self參數,只是把它當作一個標準的位置論據。

相關問題