python3 TypeError: 'function' object is not iterable
108,936
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.
Author by
Droid
Updated on July 09, 2022Comments
-
Droid almost 2 years
What change is required in the source code?
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(): return Update elif code == 'n' or code == 'N': return else: print("Warning!") AptUpdate() exception: Traceback (most recent call last): File "pybash.py", line 110, in AptUpdate() File "pybash.py", line 102, in AptUpdate for i in Update: TypeError: 'function' object is not iterable
-
Martijn Pieters about 10 yearsYour traceback and actual code do not match. The answer lies in the exact difference between the two.
-
Martijn Pieters about 10 yearsYour next error will be
'NoneType' object is not iterable
, asUpdt
doesn't actually return anything useful. -
jfs about 10 yearsyou should probably read Difference between returns and printing in python? and Python: What is the Formal Difference Between Print and Return?
-
ElMaestroDeToMare over 2 yearsYou don't return anything. You should return an iterable object such an array or a list, etc
-
-
Rushy Panchal about 10 yearsIf 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.
-
user3460822 about 10 yearsThat 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.
-
Yvonne Aburrow almost 7 yearsI 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 beroutes = db.get_all_routes()
(new line)for route in routes: