[Solved] python3 TypeError: ‘function’ object is not iterable

What change is required in the source code?

    def Update():
        cmd = os.system('xterm -e apt-get update')
        print("Finish update")

    def AptUpdate():
        print("Update system? {Y/N}")
        print("Y or y")
        print("N or n")
        code = input("Command > ")
        if code == 'y' or code == 'Y':
            for i in Update():
                return Update
            elif code == 'n' or code == 'N': 



    Traceback (most recent call last):
      File "", line 110, in 
      File "", line 102, in AptUpdate
        for i in Update:
    TypeError: 'function' object is not iterable

Enquirer: Droid


Solution #1:

What the traceback error is pointing out is the misuse of for statement:

for i in Updt():

for in python 3 is as follows: “Python’s for statement iterates over the items of any sequence (a list or a string), in the order that they appear in the sequence.” (source: python 3.3 documentation, section 4: More control structures Python 3

Since a function is neither a list nor a string, you can’t use the format:

for [variable] in [function]():

As far as what needs to be fixed, it depends on what those two functions are supposed to accomplish individually.

Respondent: Droid

Solution #2:

So let’s break down the error.

TypeError: ‘function’ object is not iterable

This error points to the line

for i in Update()

You always want the “thing” after in to be an iterable, meaning something that you can loop through. Because you don’t actually return anything in your Update() function. Python tries to loop through an object of type NoneType which is not allowed.

Respondent: user3460822

The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Most Popular

To Top
India and Pakistan’s steroid-soaked rhetoric over Kashmir will come back to haunt them both clenbuterol australia bossier man pleads guilty for leadership role in anabolic steriod distribution conspiracy