1

What change is required in the source code?

def Update():
    print("update called")

for i in Update:
    print(i)

Exception:

Traceback (most recent call last):
  File "example.py", line 4, in <module>
    for i in Update:        
TypeError: 'function' object is not iterable
Mateen Ulhaq
  • 24,552
  • 19
  • 101
  • 135
Droid
  • 1,410
  • 8
  • 23
  • 37
  • 4
    Your traceback and actual code do not match. The answer lies in the exact difference between the two. – Martijn Pieters Mar 25 '14 at 11:46
  • 6
    Your next error will be `'NoneType' object is not iterable`, as `Updt` doesn't actually return anything useful. – Martijn Pieters Mar 25 '14 at 11:47
  • 3
    you should probably read [Difference between returns and printing in python?](http://stackoverflow.com/q/3881434/4279) and [Python: What is the Formal Difference Between Print and Return?](http://stackoverflow.com/q/7664779/4279) – jfs Mar 25 '14 at 12:11
  • You don't return anything. You should return an iterable object such an array or a list, etc – ElMaestroDeToMare Oct 05 '21 at 15:04

3 Answers3

7

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 tutorial, 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.

MattDMo
  • 100,794
  • 21
  • 241
  • 231
  • 2
    If the function returns an iterable-type, then this can be used. His issue is that in his actual code (based on the error), he never calls the function, but attempts to iterative over a reference of the function itself. – Rushy Panchal Mar 25 '14 at 19:29
  • That is true, I suppose the purpose of the block given is confusing to me because I don't do a whole lot of shell scripts. – user3460822 Mar 25 '14 at 19:53
  • 3
    I forgot to put the () on the end of my function call, so Python threw the same error. `routes = db.get_all_routes` (new line) `for route in routes:` should be `routes = db.get_all_routes()` (new line) `for route in routes:` – Yvonne Aburrow May 12 '17 at 13:10
0

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 Object 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.

Neel Sandell
  • 429
  • 5
  • 12
0
import os

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


def AptUpdate():
    print('\n')
    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():
            print(Update)
    elif code == 'n' or code == 'N':
        return
    else:
        print("Warning!")


AptUpdate()

Prints "Finish update" but you will get another error