随着自动化脚本的增多,框架变得臃肿,有些公用函数改动变得困难。
现在想在运行时,打印出脚本的文档注释,但不能所有脚本都改一次。
所以在入口函数中加入:
inspect.getmodule(inspect.stack()[1][0]).doc
inspect.stack()取该函数的栈信息,第一个是函数本身的信息,第二个是调用这个函数的调用者的信息。
通过inspect.getmodule()方法,取调用者的模块对象,然后打印出doc属性就可以了
举例:
test_1.py
#!/usr/bin/python
# -*- encoding=utf8 -*-
import inspect
def b():
print("b()")
# 取模块注释
the_class = inspect.getmodule(inspect.stack()[1][0]).__doc__
print the_class
if __name__ == '__main__':
b()
test_2.py
#!/usr/bin/python
# -*- encoding=utf8 -*-
"""
取我!模块文档注释!
"""
import test_1 as t
def c():
print t.b()
if __name__ == '__mai