【发布时间】:2013-07-23 21:45:21
【问题描述】:
我正在尝试从 Python 中的字符串调用函数,如 Calling a function of a module from a string with the function's name in 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),已在类中声明。
谁能帮帮我?
【问题讨论】:
-
你能把声明的部分贴出来吗?
-
也许您应该将
iMode分配给sinx而不是sinx(t)。 -
你应该使用
self.iMode is None而不是==。 -
有关您编写的代码问题的问题必须描述具体问题并包含重现问题的有效代码。有关指导,请参阅 SSCCE.org。
标签: python string function python-2.7