我想從Python中的字符串調用函數,如 Calling a function of a module from a string with the function's name in Python中所述。從Python中的字符串調用函數會引發錯誤
不幸的是,這並不工作,Python解釋器將引發一個錯誤: TypeError: 'str' object is not callable
def current(self, t):
if self.iMode == None:
return self.i
else:
return getattr(self, 'iMode')(t)
誤差是指最後一行。 iMode
已被設置爲sinx(t)
,已在課堂上聲明。
任何人都可以幫助我嗎?
你能發表聲明的部分嗎? – zhangyangyu
也許你應該把'iMode'分配給'sinx'而不是sinx(t)。 – cdarke
您應該使用'self.iMode是None'而不是'=='。 – chepner