有这么个需求,需要用 py 脚本修改一些 py 文件内的函数 docstring, 并保存覆盖
├── test.py
├── modify.py
# test.py 待修改 docstring 的函数文件
def hello_world():
"""
this function is a demo, 需要修改这里,并保存到文件
"""
return 'hello world'
"""
# 这是一个干扰注释,不能修改这里
"""
# modify.py 修改 test.py 的 docstring 函数
from test import hello_world
def modify_docstring():
print(hello_world.__doc__) # this function is a demo, 需要修改这里,并保存到文件
hello_world.__doc__ = 'I\' am new docstring'
print(hello_world.__doc__) # I' am new docstring # 这样可以赋值,但只是在内存中,怎么修改写入 test.py 文件呢?