Python Itertools循环输出不工作

Python Itertools循环输出不工作,python,cycle,itertools,Python,Cycle,Itertools,我尝试在另一个脚本中循环使用2个python脚本,如下所示: import itertools import test import test2 x = [test, test2] cycle = itertools.cycle(x) def x(): global cycle return cycle.next() for i in range(9): y = x() print y 当我运行此程序时,它将正确打印前两行,然后接下来的7行显示 <

我尝试在另一个脚本中循环使用2个python脚本,如下所示:

import itertools
import test
import test2

x = [test, test2]

cycle = itertools.cycle(x)

def x():
    global cycle
    return cycle.next()


for i in range(9):
    y = x()
    print y
当我运行此程序时,它将正确打印前两行,然后接下来的7行显示

<module 'test' from 'C:\Users\user\Desktop\Python\test.py'>
<module 'test2' from 'C:\Users\user\Desktop\Python\test2.py'>
<module 'test' from 'C:\Users\user\Desktop\Python\test.py'>
<module 'test2' from 'C:\Users\user\Desktop\Python\test2.py'>
<module 'test' from 'C:\Users\user\Desktop\Python\test.py'>
<module 'test2' from 'C:\Users\user\Desktop\Python\test2.py'>
<module 'test' from 'C:\Users\user\Desktop\Python\test.py'>
<module 'test2' from 'C:\Users\user\Desktop\Python\test2.py'>
<module 'test' from 'C:\Users\user\Desktop\Python\test.py'>


正在搜索,但似乎找不到任何可能导致此问题的原因。

程序正在执行您告诉它的操作:

当您执行
导入测试时,在程序运行时导入test2
, 这些模块正在运行——如果它们包含顶级打印语句,则处于 此时,这些语句将生成输出

当您到达i..块的
时,程序会正确地将
y
变量,参考模块。但是像你一样打印模块对象, 不要使模块再次运行。相反,模块的字符串表示被打印出来,这就是您所看到的

为了实现我认为您想要的,您必须将您的输出封装在模块内的函数中,例如返回所需值的“evaluator”函数-/然后,只需将行从

print y


我不明白你的问题。您还有什么其他输出?您期望的输出是什么?我希望输出是这些文件在一个周期内的结果。示例:test.py结果在2中,test2.py结果在4中。此脚本的预期结果将是:2 4 2 4等。您所说的“这些文件的结果”是什么意思?test和test2在您导入它们时运行。我的意思是test.py执行“this”和test2.py执行“that”,我的questoin是如何获得上面脚本的输出,以显示“this”和“that”并重新打印,而不是看到“”
print y.evaluator()