TypeError("'bool' object is not iterable",) when trying to return a Boolean

129,915

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.

Share:
129,915

Related videos on Youtube

DominicM
Author by

DominicM

Updated on October 29, 2021

Comments

  • DominicM
    DominicM over 2 years

    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
    
    • zhangyangyu
      zhangyangyu almost 11 years
      Post the whole traceback please.
    • DominicM
      DominicM almost 11 years
      See edit for full traceback.
  • DominicM
    DominicM almost 11 years
    String method worked but is very frustrating as I have to check for string not bool in front-end. Tulepe didn't work, is this correct syntax? [[[ return ((False,)) ]]] ? Also, is it common for frameworks with routing to require iterable data types? If it's just something bottle py does I would switch to web py...
  • Marcin
    Marcin almost 11 years
    @DominicM No, you can't put a statement inside a list (or in fact, three lists). I think this is a common design. If you are trying to pass data to an XmlHTTPRequest, then I suggest you return AJAX (use the ajax module), and use an ajax parsing library on the client side.
  • DominicM
    DominicM almost 11 years
    I guess I'm used to php being able to simply return then echo true/false. What do you mean by ajax parsing library? I am currently using javascript/jquery $.ajax call.
  • Marcin
    Marcin almost 11 years
    @DominicM I mean a javascript library which will convert a string safely to the javascript objects it represents.
  • DominicM
    DominicM almost 11 years
    It seems like overkill since it's just gonna be true/false or json data for other calls.
  • Marcin
    Marcin almost 11 years
    @DominicM jquery.ajax will do it if you specify that you expect json. In any case, it's not overkill to avoid running arbitrary code. If you use eval, you're opening yourself up to XSS (and in general javascript injection attacks).
  • DominicM
    DominicM almost 11 years
    Can name a specific library as an example?
  • Marcin
    Marcin almost 11 years
    @DominicM I just told you: jquery.ajax has this functionality.
  • DominicM
    DominicM almost 11 years
    You previous comment was misleading because I already said I was using jquery's $.ajax call...

Related