我遇到了一个奇怪的问题。我有一个返回布尔值的方法。反过来,我需要再次返回该函数的结果,因为我不能直接从前端调用该方法。这是我的代码:
# this uses bottle py framework and should return a value to the html front-end
@get('/create/additive/<name>')
def createAdditive(name):
return pump.createAdditive(name)
def createAdditive(self, name):
additiveInsertQuery = """ INSERT INTO additives
SET name = '""" + name + """'"""
try:
self.cursor.execute(additiveInsertQuery)
self.db.commit()
return True
except:
self.db.rollback()
return False
这引发了一个异常:TypeError("‘pool’object is not iterable",)
我根本没有得到这个错误,因为我并没有试图"迭代"bool值,只是为了返回它
如果我返回一个字符串而不是布尔值或int值,它将按预期工作。这里可能有什么问题?
追溯:
Traceback (most recent call last):
File "C:Python33libsite-packagesbottle.py", line 821, in _cast
out = iter(out)
TypeError: 'bool' object is not iterable
查看回溯:
Traceback (most recent call last):
File "C:Python33libsite-packagesbottle.py", line 821, in _cast
out = iter(out)
TypeError: 'bool' object is not iterable
你的代码没有迭代值,但接收它的代码是。
解决方案是:返回一个可迭代的。我建议您将bool转换为字符串(str(False)
)或将其包含在元组((False,)
)中。
始终阅读回溯:它是正确的,而且很有帮助。