我想在Python中使用此代碼:使用類中的方法屬性的功能 - 蟒蛇
class A:
func = lambda: "go away"
@classmethod
def apply(cls):
cls.func()
A.apply()
我收到此錯誤:
unbound method <lambda>() must be called with A instance as first argument (got nothing instead)
我怎樣才能使它發揮作用?
它是一個類方法 - 它需要是'func = lambda cls:...'... –
您不能在Python 2.x中將lambdas聲明爲類/靜態函數。你的代碼應該在Python 3.x中工作,讓每個人都在看它。爲什麼你想這樣做呢? – zwer
在我的原始代碼中,「走開」是服務功能,在其他地方定義。 apply函數是一個使用此函數的工廠方法。 func是從外部定義的。 –