我想从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