Python一直在引用过时的变量

0 python linecache

我有一个奇怪的问题,这是我尝试解释:

我正在创建一个打开txt文件的程序,然后使用以下命令linecache.getline(path,number)读取该文件的一行,在完成该函数后,我使用commmand linecache.clearcache.

如果我然后在文本文件中更改某些内容,则会一直返回预先更改的行.

以下是我正在使用的代码(我知道它真的很漂亮)

def SR(Path,LineNumber):    
    returns = lc.getline(Path,LineNumber)      
    x = np.array([])
    y = np.array([])
    words = returns.split()
    for word in words:
        x = np.append([x],[word])

    for i in range(len(x)):
        t = float(x[i])
        y = np.append([y],[t])
    return y
    del x
    del y
    del t
    del words
    lc.clearcache()
Run Code Online (Sandbox Code Playgroud)

Sil*_*Ray 5

后没有任何return声明都不会被执行.如果要呼叫clearcache,则需要在return声明之前调用它.

另外,作为旁注,你的del陈述也不会真正做任何事情,即使它们被放置在之前return. del实际上只是递减gc中的引用计数器,这将在解释器退出函数作用域时发生.