我正在尝试重用在类的构造函数实例中声明的已打开文件,但我想我在做一些逻辑错误的事情。例如考虑以下示例
class Temp:
def __init__(self):
self.open_file_ = open('periodic_status','r')
def function1(self):
new_file = self.open_file_
for i in new_file:
print 'test1'
def function2(self):
for j in self.open_file_:
print 'test2'
if __name__ == '__main__':
obj1 = Temp()
obj1.function1()
obj1.function2()
在上面的程序中,我可以打印 test1,但无法打印语句 test2。谁能给我解释一下逻辑。
谢谢
请您参考如下方法:
