2014-03-25 98 views
0

在源代码中需要什么样的变化?python3类型错误:“功能”对象不是可迭代

 

    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 

+1

你的回溯和实际的代码不匹配。问题就出在这两个。 –

+4

之间准确的区别你的下一个错误将是''NoneType '对象不可迭代',因为'Updt'实际上不会返回任何东西有用的东西。 –

+1

你应该读作[Python中的回报与印刷之间的区别是什么?(http://stackoverflow.com/q/3881434/4279)和[Python的:什么是打印和收益之间的形式上的差别(HTTP:// stackoverflow.com/q/7664779/4279) – jfs

回答

4

什么追踪误差指出的是对声明的滥用:

for i in Updt():

for在Python 3如下:“Python的for语句依据任意序列的项目(一个列表或一个字符串),按顺序出现在序列中。“ (来源:蟒蛇3.3文档,第4节:更多的控制结构Python 3

由于功能既不是列表也不是一个字符串,你不能使用格式:

for [variable] in [function]():

至于什么需要固定,要看是什么这两个功能都应该独立完成。

+1

如果该函数返回一个可迭代型,那么这可以被使用。他的问题是,在他的实际代码中(基于错误),他从不调用函数,而是试图迭代函数本身的引用。 –

+0

这是真的,我想给块的目的是迷惑我,因为我没有做一大堆的shell脚本。 – user3460822

+0

我忘了把()放在函数调用的最后,所以Python抛出了相同的错误。 '路由= db.get_all_routes'(换行) '用于路线路线:'应该是'路由= db.get_all_routes()'(换行) '用于路线路线:' –

相关问题