gpt4 book ai didi

python - 我怎样才能让 inspect.getsource 响应源代码的变化?

转载 作者:行者123 更新时间:2023-11-28 22:55:46 29 4
gpt4 key购买 nike

我做了两个文件:

#test_func.py
def test():
print('hello')

#test_inspect.py
import inspect
import test_func

reload(inspect)
reload(test_func)
reload(inspect)
reload(test_func)

print inspect.getsource(test_func.test)

从 IPython 或其他交互式 shell 运行 import test_inspect 打印正确的东西:

def test():
print('hello')

但如果我将 test_func.py 编辑并保存为:

#test_func.py
def test():
print('good bye')

我仍然得到:

def test():
print('hello')

当我从 shell 运行 reload(test_inspect) 时。我怎样才能说服 inspect 模块重新读取源文件?

(如果您必须知道我为什么要这样做,我会在评论中详细说明,但现在,我只想知道是否有解决方法,或者是否inspect 模块有一些基本的东西可以防止这种情况发生)

最佳答案

这应该可以解决问题:

import linecache
linecache.clearcache()

关于python - 我怎样才能让 inspect.getsource 响应源代码的变化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16409385/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com