我是 python 的新手,突然惊讶地看到一个变量在声明和分配它的块之外仍然可见。代码如下:
with open('data.txt', 'r') as file:
text = file.readlines()
# and when do I close the file?
for i in range(len(text)): # still can access the text even after the block.
print(text[i])
这怎么可能从块外部看到变量?提前致谢。
扬帆大鱼
相关分类