当试图返回一个布尔值时,出现类型错误("'bool'对象不是可迭代的",)[英] TypeError("'bool' object is not iterable",) when trying to return a Boolean

本文是小编为大家收集整理的关于当试图返回一个布尔值时,出现类型错误("'bool'对象不是可迭代的",)的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我有一个奇怪的问题.我有一种返回布尔值的方法.反过来,我需要该功能的结果再次返回,因为我无法直接从前端调用该方法.这是我的代码:

# 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("'bool'对象是不可能的",)

我根本没有出现此错误,因为我没有尝试"迭代"布尔值,而只是返回.

如果我返回字符串而不是布尔值或int,则可以按预期工作.这里可能有什么问题?

追溯:

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

推荐答案

查看追溯:

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

您的代码没有迭代该值,但是接收的代码是.

解决方案是:返回一个值得一提的问题.我建议您要么将布尔转换为字符串(str(False)),要么将其包装在元组中((False,)).

始终阅读跟踪:这是正确的,这很有帮助.

本文地址:https://www.itbaoku.cn/post/1975364.html

问题描述

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",)

I don't get this error at all since I am not attempting to "iterate" the bool value, only to return it.

If I return a string instead of boolean or int it works as expected. 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,)).

Always read the traceback: it's correct, and it's helpful.