我使用@classobj
實現了幾個構造函數。我不僅設置變量,但還呼籲在新的類方法:類方法構造函數的類型錯誤
class Example:
def __init__(self):
pass
@classmethod
def constructor1(cls,x,y):
self=cls
self.__x = x
self.__somemethod(self,y)
...
我得到以下錯誤:
unbound method __somemethod() must be called with Example instance as
first argument (got classobj instance instead)
我該如何解決這個問題呢?
使用'cls'而不是'self'然後做'self = cls'的原因是什麼? –
你的用例是什麼 - 你是否試圖使用'@ classmethod'作爲工廠函數? –