背景:
想引入另一个python文件中所有的内容(类或方法),但是又想自定义其中的某些方法
代码:
import importlib.util
spec = importlib.util.spec_from_file_location("python文件的文件名(不带.py),比如get_results", "python文件的路径(带.py)")
get_qwen_results = importlib.util.module_from_spec(spec)
spec.loader.exec_module(get_qwen_results)
def renew():
pass
get_results.old = renew 即可把原来文件中的“old”方法替换成“renew”了
此时再调用 get_result.main(假设这个main函数在原py文件中调用了 old 这个方法) 那么使用的函数就会替换成renew了