[英]TypeError(“'bool' object is not iterable”,) when trying to return a Boolean
I am having a strange problem. 我有一个奇怪的问题。 I have a method that returns a boolean. 我有一个返回布尔值的方法。 In turn I need the result of that function returned again since I cant directly call the method from the front-end. 反过来我需要再次返回该函数的结果,因为我无法直接从前端调用该方法。 Here's my code: 这是我的代码:
# 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
This throws an exception: TypeError("'bool' object is not iterable",) 抛出异常:TypeError(“'bool'对象不可迭代”,)
I don't get this error at all since I am not attempting to "iterate" the bool value, only to return it. 我根本没有得到这个错误,因为我没有试图“迭代”bool值,只是为了返回它。
If I return a string instead of boolean or int it works as expected. 如果我返回一个字符串而不是boolean或int它按预期工作。 What could be an issue here? 这可能是个问题?
Traceback: 追溯:
Traceback (most recent call last):
File "C:\Python33\lib\site-packages\bottle.py", line 821, in _cast
out = iter(out)
TypeError: 'bool' object is not iterable
Look at the traceback: 回顾一下追溯:
Traceback (most recent call last):
File "C:\Python33\lib\site-packages\bottle.py", line 821, in _cast
out = iter(out)
TypeError: 'bool' object is not iterable
Your code isn't iterating the value, but the code receiving it is. 您的代码不是迭代值,而是接收它的代码。
The solution is: return an iterable. 解决方案是:返回一个可迭代的。 I suggest that you either convert the bool to a string ( str(False)
) or enclose it in a tuple ( (False,)
). 我建议您将bool转换为字符串( str(False)
)或将其包含在元组中( (False,)
)。
Always read the traceback: it's correct, and it's helpful. 总是阅读回溯:它是正确的,它是有帮助的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.